How do I turn on and turn off parts of my script?
26 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jenna Wong
el 31 de Oct. de 2018
Comentada: Image Analyst
el 2 de Nov. de 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 comentarios
Stephen23
el 2 de Nov. de 2018
Editada: Stephen23
el 2 de Nov. de 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
el 2 de Nov. de 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"
Respuesta aceptada
the cyclist
el 31 de Oct. de 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 comentarios
Más respuestas (2)
Image Analyst
el 31 de Oct. de 2018
At the start of the code you can use %{ then at the end %}
%{
Stuff to be commented out
%}
0 comentarios
the cyclist
el 31 de Oct. de 2018
Editada: the cyclist
el 31 de Oct. de 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 comentarios
Image Analyst
el 31 de Oct. de 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
el 2 de Nov. de 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.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!