How do I turn on and turn off parts of my script?

24 views (last 30 days)
(I am sure there are already answers to my question, but I am having trouble thinking of the keywords that would lead me to them.)
I have written a long script for a project that has multiple parts. In order to demonstrate each part, I have to comment out some other parts and uncomment the part that I need.
The project is too complicated for me to consider putting everything in functions only. Is there a way for me to easily turn on and off parts of my script?
Edit: My code is divided into sections and I can comment and uncomment out blocks of it, but I was told that the code needed to have "flags" so that I can easily turn on and off different blocks of the code with just a click or a single input. A function or an if statement might work but I think that would be very messy.
  3 Comments
Stephen23
Stephen23 on 2 Nov 2018
Edited: Stephen23 on 2 Nov 2018
"How do I turn on and turn off parts of my script?"
"I was told that the code needed to have "flags" so that I can easily turn on and off different blocks of the code with just a click or a single input"
Sure, this is easy: just use if, switch, and any other basic programming flow control operations. That is exactly what they are for. There is no point in reinventing the wheel.
Image Analyst
Image Analyst on 2 Nov 2018
In case you're not familiar with the terminology, a variable or expression or condition that an "if" statement tests is often called a "flag". So make a variable, or a boolean expression with operators like <, >, =, &&, and , that evaluates to false (or 0) or true (or non-zero) and that can be your "flag"

Sign in to comment.

Accepted Answer

the cyclist
the cyclist on 31 Oct 2018
I don't have much experience with this new(ish) functionality, but it sounds like you might be describing things that can be done with a Live Script.

More Answers (2)

Image Analyst
Image Analyst on 31 Oct 2018
At the start of the code you can use %{ then at the end %}
%{
Stuff to be commented out
%}

the cyclist
the cyclist on 31 Oct 2018
Edited: the cyclist on 31 Oct 2018
In the Editor tab (of the Editor window), there is a Comment functionality.
Highlight the section(s) of code you want to comment out, and click that button.
You could also divide your code into Code Sections.
  6 Comments
Image Analyst
Image Analyst on 31 Oct 2018
What does automatic mean to you? You could put an if test in there, that checks some condition such as whether the user checked a checkbox on the user interface.
if someCondition
% Code to execute....
end
Again, explain what automatic means. Under what condition would the "automatic" process include (run) or exclude (not run) code?
Stephen23
Stephen23 on 2 Nov 2018
"I would just like to know if there is a faster, more automatic way to turn on and off certain parts of the script..."
The fastest, simplest, and most intuitive solution is to use if statements.

Sign in to comment.

Categories

Find more on Scripts in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!