1

I like to plot things when I'm testing, but for the full run I would like the plotting to be turned off in an easy and efficient way. Currently I have a variable at the top of the script as follows

plotting = true;

Then, in all the sections with plotting, I have something along the lines of

if plotting
    figure;
    plot(x,y)
    ...
end

So of course if I don't want to plot, I simply set plotting = false; at the top of the script.

Is there a better, more efficient way of doing this? Or does anyone have a method that they use that is different?

Wolfie
  • 27,562
  • 7
  • 28
  • 55
user3376851
  • 149
  • 10
  • AFAIK there's no better method. I sometime use a variable which can have integer values, e.g. `verbose=0` gives no status messages, `verbose=1` gives text messages and `verbose=2` gives text and plots. – Adriaan Jul 17 '18 at 12:44
  • This method is fine. What don’t you like about it? – Cris Luengo Jul 17 '18 at 12:47
  • I agree with previous comments. Your way might be the best. If you want to clean your script, maybe you can define the if-statement in a function; maybe call it plot_if(x,y,plotting). – Juancheeto Jul 17 '18 at 12:53
  • I guess I don't necessarily think there's anything wrong with it, just was wondering if there was another way as it had never occurred to me to think of anything different. E.g if there was a line of code you could type at the top of the script that would then ignore all figures leaving no need to go through and alter the code (I ended up with a lot of different places where plotting was occurring so it seemed clunky going through and adding the if statement everywhere which is the first time I'd noticed a problem with this method) – user3376851 Jul 17 '18 at 12:54
  • 1
    If you are on Windows you could use `matlab -noFigureWindows` option when starting Matlab. When you are testing start Matlab normally and when you want to do the full run use the noFigureWindows startup option. More info [here](https://uk.mathworks.com/matlabcentral/answers/98969-how-can-i-temporarily-avoid-figures-to-be-displayed-in-matlab) – Paolo Jul 17 '18 at 14:14
  • Possible duplicate of [Disable plots in Matlab](https://stackoverflow.com/questions/10129622/disable-plots-in-matlab) – Wolfie Jul 18 '18 at 14:07
  • I agree that is essentially a duplicate of the question. However I believe the solutions presented in the answers here are better as the main answer from that was regarding disabling the plots but they still run in the background which isn't ideal – user3376851 Jul 19 '18 at 06:50

2 Answers2

4

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.

Wolfie
  • 27,562
  • 7
  • 28
  • 55
  • I'm not sure I like comparing a boolean with `true`. But I do like your second suggestion. An alternative could be to use a list of figure handles, one for each plot in the file. Fill in 0 if you don't want the plot. `plotting = [1,2,0,1];`, and later `if plotting(2), figure(plotting(2)), ... end`. Then you can have some plots overwriting or adding to other plots. – Cris Luengo Jul 17 '18 at 17:23
  • @Cris yeah but I think the comparing to true is neater than suppressing the warning and neater than having the warning! That array is a nice idea, in the past I've also used a cell array of actual figures so you don't have to reference them by number – Wolfie Jul 17 '18 at 18:24
  • Thanks for your response I like that idea though I guess in principle is still the same method. Good spot regarding the editor warnings – user3376851 Jul 18 '18 at 09:19
  • 1
    @Wolfie I downvoted because I believe that your answer, just like the answer below, does not solve OP's problem. Difference being that this answer got six upvotes while the one below got none, which I guess tells you a lot about this community. As OP states in the comment above, this answer effectively does the same thing as the code presented in the question. OP's is asking for a more efficient solution to turn off **all** plotting, which is why I suggested starting Matlab with the `-noFigureWindows` flag. – Paolo Jul 18 '18 at 13:15
  • @pkpkpk: “which tells you a lot about this community” — do you mean that we can tell the difference between a good solution and a bad solution? Up voting the other answer would mean agreeing to leave debug plotting functionality running, but invisibly in the background, in production code. You can’t compare that to this answer. — I also don’t like your suggestion a whole lot, because it disables all plotting, not just the debug plotting. But if you had written it as an answer I would have upvoted it too, because it could be a solution for someone. – Cris Luengo Jul 18 '18 at 13:28
  • @Cris No I do not mean that, because if that was true then this answer would not have received so many upvotes, as, objectively, it does not solve the problem. The other answer was also not worthy of upvotes, in fact it was probably worthy of downvotes as that failed to answer OP's question. What I do mean is that this community is small and often not so welcoming of new users. Had this answer been posted by RadioJava below, do you believe his answer would have received the same treatment? – Paolo Jul 18 '18 at 13:43
  • @pkpkpk In the first line, the actual "answer" is *your current method is about as good as it gets*. I then elaborated on alternative options. I also acknowledged that the 2nd method isn't more efficient, but has advantages over the OP's method. The OP correctly noted that the principal is the same, there is no argument to be had on that point! I'm not sure what you're trying to say w.r.t if this answer had been posted by someone else? Thanks for leaving a comment explaining the downvote, it's more constructive so that we can use downvotes as a catalyst for improving. – Wolfie Jul 18 '18 at 13:52
  • @pkpkpk, it is true, this is a small community. This is why I’m actually more likely to upvote answers from new users—I’d love to see this community be larger, and like to encourage people to post. Other than that, I upvote answers that I learn something from, or that are correct, well written and thorough. I would not have upvoted this post if it just answered the question (i.e. if it were just the first sentence). – Cris Luengo Jul 18 '18 at 14:00
-1

You can add the line:

set(0,'DefaultFigureVisible','off')

At the beginning of your code to keep figures hidden.

Undo this by setting:

set(0,'DefaultFigureVisible','on')

Note that the figures and plots are still created, just hidden.


Example:

set(0,'DefaultFigureVisible','off') % Visible off

figure(6); % This figure will be created but hidden

plot(6:8); % Plot is created but on the hidden figure

set(0,'DefaultFigureVisible','on') % Visible on

figure(6); % The figure will now be shown, with the previous plot.
Prostagma
  • 1,756
  • 9
  • 21
  • Smart! Does this not calculate the figures at all, or does it only not display them? This might be interesting if you're e.g. trying to make a scatter plot with 1M points, since that will take a long time to render even without actually opening the figure window. – Adriaan Jul 17 '18 at 13:02
  • 2
    The plotting still happens whether you see the result or not, so a complex plot would still delay your program. Also hidden figures can be forgotten about and cause further slow-downs if you forget about them. This isn't really solving the same problem of not plotting when not testing, in fact you'd want to avoid just creating a bunch of hidden figures in non-test code! – Wolfie Jul 17 '18 at 13:07
  • I mentioned what you said in the answer. This doesn't improve runtime but allows stopping the figures from popping, with a single line at the top. A 'close all' can be added at the end to avoid forgetting about hidden figures – Prostagma Jul 17 '18 at 13:14
  • 1
    One extension, if you did want to use this method, would be to use some variable say `plotting = 'on'`, then some figures can always be shown with `figure(n)`, and optional figures can be hidden or shown depending on the value of `plotting`. You wouldn't use the `DefaultFigureVisible` option, instead with each optional figure you'd use `figure(n, 'visible', plotting)`. Also, you would need to use **`close all hidden`** to close all figs including those with hidden handles. – Wolfie Jul 17 '18 at 13:27