How do I turn on and turn off parts of my script?
26 次查看(过去 30 天)
显示 更早的评论
(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 个评论
Stephen23
2018-11-2
编辑:Stephen23
2018-11-2
"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
2018-11-2
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"
采纳的回答
the cyclist
2018-10-31
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 个评论
更多回答(2 个)
Image Analyst
2018-10-31
At the start of the code you can use %{ then at the end %}
%{
Stuff to be commented out
%}
0 个评论
the cyclist
2018-10-31
编辑:the cyclist
2018-10-31
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 个评论
Image Analyst
2018-10-31
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
2018-11-2
"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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!