How do I turn on and turn off parts of my script?
23 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Jenna Wong
am 31 Okt. 2018
Kommentiert: Image Analyst
am 2 Nov. 2018
(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 Kommentare
Stephen23
am 2 Nov. 2018
Bearbeitet: Stephen23
am 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
am 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"
Akzeptierte Antwort
the cyclist
am 31 Okt. 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.
0 Kommentare
Weitere Antworten (2)
Image Analyst
am 31 Okt. 2018
At the start of the code you can use %{ then at the end %}
%{
Stuff to be commented out
%}
0 Kommentare
the cyclist
am 31 Okt. 2018
Bearbeitet: the cyclist
am 31 Okt. 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.
6 Kommentare
Image Analyst
am 31 Okt. 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
am 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.
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!