Disable plots in Matlab
Asked Answered
M

3

11

I have some programs written in Matlab that I need to run several times for some reasons (debugging, testing with different input, etc...)

But, there are a lot's of graphs that are plotted by the programs and its various functions such that everytime I run the program, I have to wait for all the graphs to be displayed, which is very annoying and time consuming (especially when you are working with a small laptop). After the program is executed, I close them with a close all.

So my question is:

Is there a way to disable all plots/figures/graphs in Matlab? either in the options, or by executing a certain code like disable plot and enable plot to ensure that no figures are being displayed.

I know that I can just browse the code and comment the plotting part, but I don't want to forget to uncomment.

Mettlesome answered 12/4, 2012 at 18:32 Comment(0)
P
14

Try some combination of the two commands:

set(gcf,'Visible','off')              % turns current figure "off"
set(0,'DefaultFigureVisible','off');  % all subsequent figures "off"

The second one, if you put it near the beginning of your program, might do the trick for you. Of course, it is still creating the plots, which might be undesirable for computation time and/or RAM issues.

This is a classic reason to avoid Matlab when one can. It fosters bad programming design. To solve this problem correctly, you should create something that lets you "flip a switch" at the highest level of your program and control whether plots show or do not show. Perhaps it even has gradations of the show/don't show option so you can select different types of plots that do/do not show depending on what diagnostics you are running.

Ideally, you'd want this "flip a switch" creation to be a class that has access to visibility and plot functions of other objects. But because interactive object-orientation is so cumbersome in Matlab, it's often not worth the effort to develop such a solution, and most people don't think about this design aspect from the outset of their project.

Matlab would encourage someone to solve this by making flag variables like "isPlotVisible" or something, and creating functions that always accept such flags. I agree this is a bad design.

Perfection answered 12/4, 2012 at 18:43 Comment(5)
ah, nice. Didn't know about those.Dockage
The same thing will work in Octave as well, which is remarkable since plotting is one of the ways that Octave usually differs from plain Matlab functionality.Perfection
The set(0,'DefaultFigureVisible','off'); is just perfect! It greatly improved the performance of the program. It's like it was spending more time creating the windows for the figures than actually doing real computation.Mettlesome
@olchauvin: Note that EMS implied that the plots are still being created ("it is still creating the plots, which might be undesirable for computation time and/or RAM issues.") A simple close all, run periodically, might be useful to clean up these invisible plots if they're using up a bunch of resources.Adverbial
@Li-aung Yip: Yes, thank you. Since I have a close all at the beginning of the program, it will be fine.Mettlesome
D
3

You could run matlab from the command line with:

matlab -nojvm

but then you don't get the GUI at all. Alternatively, you could write a file 'plot.m':

function h = plot(varargin)
  h = [];
end

which doesn't do anything. If this is in the working directory (or somewhere else near the top of the path), then plot will call your function instead of the 'real' plot. You'd need to do the same from any other graphing functions you call.

The closest way I know of 'turning off plotting' would be a folder of such functions that you can add to the path to disable plotting, and remove to enable.

Dockage answered 12/4, 2012 at 18:43 Comment(2)
+1 creative solution, even though it's horrible (INTENTIONALLY shadowing MATLAB builtins?)Adverbial
yup, and even worse since I recently chastised someone for giving an answer that would make Doug Hull cryDockage
E
1

The previous methods are fine, but an easy and good habit to take is to use a "on/off parameter". So basically, at the beginning of your code, you can add something like:

DisplayFigure = 1; %1 = display, 0 = no display

After that, add "if DisplayFigure == 1 ... end" for all your plotting related commands, where the commands should be inside the if statement (the ... above). Hence you won't even compute the plots, which will save you a lot of time and memory. You just have to change the value of the variable "DisplayFigure" to plot or not the figures.

Energetic answered 5/2, 2015 at 1:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.