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

24 visualizzazioni (ultimi 30 giorni)
(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 Commenti
Stephen23
Stephen23 il 2 Nov 2018
Modificato: Stephen23 il 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 il 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"

Accedi per commentare.

Risposta accettata

the cyclist
the cyclist il 31 Ott 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.

Più risposte (2)

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

the cyclist
the cyclist il 31 Ott 2018
Modificato: the cyclist il 31 Ott 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 Commenti
Image Analyst
Image Analyst il 31 Ott 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 il 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.

Accedi per commentare.

Categorie

Scopri di più su Scripts in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by