Matlab plots with a very low resolution when using -nodesktop option - matlab

I'm having trouble with the resolution of matlab plots when the function is run via a cronjob using the -nodesktop option. The function plots, and saves (using the print function) a number of figures. When run from the matlab desktop, they plot and save according to the specified resolutions with no problems. But when run via a cronjob with the -nodesktop option, the resolutions are very poor (low).
This is essentially what I'm currently doing:
fh = figure;
set(fh,'Position',[0 0 1360 470]);
plot(somedata);
print(fh,'-dpng',figpath,'-painters');
Note that I've also tried specifying a print resolution:
print(fh,'-dpng',figpath,'-r300');
But this ends up with corrupt files. I'm not sure why. I'm using -painters because I read somewhere that if the plot is somewhat complicated it can default to opengl which ruins prints but this doesn't seem to be the issue (with or without the option, the plots are the same).
I've also tried:
set(fh,'PaperPositionMode','auto')
which does not solve the problem.
The cronjob runs the following command:
/usr/local/bin/matlab -nodesktop -r "startup; perform_plots; exit"
Any help would be much appreciated!

I figured out the issue.
By running matlab as a cronjob, the DISPLAY environmental variable wasn't set. As a result there was no X display for matlab to utilise. I don't know how it still managed to plot anything at all but it did and did so with a very low resolution.
The solution is to set the appropriate display before hand. e.g.:
#!/bin/bash
export DISPLAY=:1.0
/usr/local/bin/matlab -nodesktop -r "startup; perform_plots; exit"
In case you want to run it on a box that doesn't have an X server, you can set up a dummy X server using Xvfb (X virtual frame buffer). This also has the advantage of using a display separate to the one you may be working on so jobs can run in the background without plots randomly appearing while you work.

Related

X11 MATLAB Display Figure

I know it's possible to forward any output from a remote machine to a local one by using the X11 forwarding remote tunnelling, so that when you run a MATLAB command it will display all the graphical outputs back to the machine you've connected from.
My question is:
Is there any MATLAB command to just output the figures (e.g., plot,surf,etc.) without displaying any other graphical object (i.e., the main interface)?
In practice, I would like to interact with MATLAB by using the command line (as shown below) and forward back only the figures.
MATLAB cannot display figures without its own figure-GUI, so the answer to your question would be no.
However: there is a workaround: create an invisible figure using f=figure('visible', 'off'), then plot your data, and finally use saveas(f,filename,fileextention). Don't forget to close(f) your figure after saving, to free the RAM. You'll now have a figure in your file directory, which you can display using your favourite visualising tool, which might even be possible through a call to system, although I have never tested that.

MATLAB, overwriting in command window

I am running some code in MATLAB and basically there is a lot of output, that is going into the MATLAB command window.
However, when it gets so many thousand, it starts to overwrite, so the output at the very start disappears from the MATLAB command window?
Any suggestions?
Thanks in advance!

Disable Matlab focus stealing, without modifying Matlab script

I hava Matlab script running in the background. It keeps popping up pogress bar windows as the computation progresses. This is very annoying.
Problem is I did not write the Matlab script myself, and it is a very long and complicated piece of code that I don't want to mess with. So how can I prevent Matlab from stealing focus, without modifying the Matlab script? Hopefully some Matlab setting will let me do this, without modifying the script itself.
In case it matters, my PC is running Xubuntu.
Some ideas to avoid figures:
Open a single worker (requires parallel computing) and run your script on it. Workers automatically don't have a GUI
matlabpool 1
warning('off','MATLAB:Completion:AllInputsExcluded') %turn off warning
spmd, yourfunction, end
Use the matlab startup parameters to disable figures
matlab -noFigureWindows
or start matlab as a command line tool, running your function, saving the workspace and exiting.
matlab -nodisplay -nosplash -r "yourfunction;save('result.mat');exit"

Disable plots in Matlab

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.
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.
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.
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.

Increase matlab figure resolution, when saving in headless mode

I have a plot, which I want to save with a bigger resolution. But the documentation states that Matlab ignores '-r' option to print command, when running in a headless mode. Is there some other way to increase printed figure resolution?
You could use the function export_fig written by Oliver Woodford. Its -r option (resolution) also works in headless mode. The function is available here.
As suggested above, export_fig provides a solution, but in later versions of Matlab, it may take a fair amount of tinkering to get it working.
export_fig(gcf, 'figure.png', '-png','-painters','-m2');
Should do the trick. The critical parameters here are:
'-painters' which forces matlab to use the correct rendering engine. The default (OpenGL) does not work in -nodisplay mode and "zbuffer" is no longer supported in recent version of matlab.
'-m2' which indicates output at x2 resolution. You can obviously specify '-m3' for x3 resolution and so on.