Skipping part of code

27 visualizaciones (últimos 30 días)
Matteo Tesori
Matteo Tesori el 23 de Mayo de 2023
Comentada: Walter Roberson el 23 de Mayo de 2023
I've wrote a script that can be splitted in independent parts. Sometimes I don't want to execute all of them, and so I'm using on each part of the script a simple check of the type
if flag
% do what you have to do
end
where flag can be zero (if I want to skip the current part) or one (if I want to execute the current part).
This trick works fine but is pretty annoying because when I set a zero flag the analyzer returns a warning. I don't want suppress such warning of the analyzer, so I'm looking for a new solution (entirely procedural) that does not generate any warning.
  2 comentarios
dpb
dpb el 23 de Mayo de 2023
if flag
% do what you have to do
else
end
will probably be enough to suppress the warning. You can also suppress the warning on the specific line(s) with the %#ok comment/directive.
Nathan Hardenberg
Nathan Hardenberg el 23 de Mayo de 2023
putting the else statement still produces the warning. But good to know with the %#ok comment

Iniciar sesión para comentar.

Respuesta aceptada

Nathan Hardenberg
Nathan Hardenberg el 23 de Mayo de 2023
You could do it with a while loop. This does not produce a warning. And don't forget to put in the break statement at the end of the loop 😉
flag = 0;
while flag
% do what you have to do
a = 123
break
end
disp('end')
end

Más respuestas (1)

dpb
dpb el 23 de Mayo de 2023
The other way that fools the code analyzer is to insert one level of indirection...the following doesn't generate the warning whether DBG is True or False...
DEBUGFLAG=[True|False]; % set the state desired
flag=DEBUGFLAG; % assign state to the logic variable..
....
if flag
% do what you have to do
end
The possible problem with the above is, of course, if you have multiple sections to set independently.
I'd probably just choose to ignore the warnings for a case like this since know what it is am trying to do.
  1 comentario
Walter Roberson
Walter Roberson el 23 de Mayo de 2023
Ah yes, the technique of baffling the analyzer with bovine excrement ;-)

Iniciar sesión para comentar.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by