As mentioned in the comments, your current method is about as good as it gets... Here is a note on that method and an alternative though.
Keeping the editor warnings tidy
The MATLAB editor will underline the first line in your if
statement if you use the syntax you've show, i.e. no comparison operator on a Boolean:
plotting = false;
if plotting
figure % <- this line will be underlined orange
% as the editor "knows" it will never be reached!
% ...
end
A quick fix is to use an equals comparison (==
) which the editor doesn't check in the same way. This is also more explicit and slightly clearer for future reference:
plotting = false;
if plotting == true
figure % <- this line is now not highlighted
% ...
end
Using figure-number arrays
You use the word "efficient" in your question. You won't find a more efficient method than the two-liner above, but you might want to play around with arrays of figures. This method allows you to specify certain figures for plotting, and means you can have an array of optional figures:
plotting = [1 3]; % We want to plot figures 1 and 3
if any(plotting == 1)
figure(1); % do stuff with figure 1
end
if any(plotting == 2)
figure(2); % won't enter this condition because plotting = [1 3]
end
if any(plotting == 3)
figure(3); % do stuff with figure 3
end
If you don't want to plot anything, simply set plotting = []
;
Note that if you had many similar figures then the 3 above conditionals could be placed in a simple loop, with minor variations (dictated by further if
statements) in each plot.