Increase matlab figure resolution, when saving in headless mode - matlab

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.

Related

Running Dynare from Matlab

I am new to Dynare++ and I have really quick question I cannot seem to find the answer too.
What is the difference between these two commands and why is the output different?
!dynare++ --per 50 --sim 3 file_name.mod
dynare file_name.mod
In the first command its unable to find steady state values based on my initial values and in the second it can. Why?
The first command calls the standalone Dynare++ instead of Matab-based Dynare. The latter uses Matlab's solvers for numerically finding the steady state. Note that there is a dedicated forum for Dynare at https://forum.dynare.org

Close a Simulink mask using M-Code

I have been looking for a solution on the mathworks but found nothing. I need to control opening and closing of a simulink mask using code.
I can open the mask with:
open_system(gcb, 'mask')
But I cannot close it with:
close_system(gcb, 'mask')
Solution must work on matlab 2011b & 2014b
Any clue ? I need to support a block on both matlab 2011b and 2014b and the work flow and behavior are different, forcing me to control a mask close/open under certain circumstances.
Found it,
close_system(gcb)
Is enough. Don't know why it didn't work the first time however.

Setting Advance Solver settings in Dymola

Is there a way to set the maximal solver stepsize (hmax) directly in Dymola?
The value for hmax can be changed in dsin.txt and it works if the simulation is run with dymosim.exe, but gets overwritten if simulation is started in Dymola.
Is there some hidden flag that I can use?
Thanks!
After editing dsin.txt, try to execute dymosim.exe directly instead of using the GUI (without parameters e.g. when executing via a double-click from the explorer this will create dsres.mat instead of <modelName>.mat). This should prevent Dymola from re-creating dsim.txt and therefore consider your setting for hmax.
Not very convenient, but currently the only way I know to make it work.
You can as well create dsin.mat and provide it as an argument for dymosim.exe to be able to re-create dymosim.exe using the GUI, without modifying dsin.txt every time. This is shown here: dymola.readTrajectory - different sample times. You need to skip the second parameter "<DymolaWD>\tgrid.mat" obviously.
#Shanga: I think this should work for eviter as well, but I have no model to reliably test it. So feedback would be appreciated...
Setting maximum stepsize is only reasonable for solvers without stepsize control, e.g. Euler or Runge-Kutta. In those cases, a Fixed Integrator Step can be set in Simulation setup.
Currently there is no convenient way to do this directly from Dymola. The dsin.txt file has to be edited as you describe.
I believe you set it through the parameters Interval length or Number of intervals in the General section of the Simulation Setup. The system will be solved at least on those points, which I understand as the maximum stepsize.

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

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.

diagnostic for MATLAB ODE

I am solving a stiff PDE in MATLAB using ode15, and it often freezes depending on the initial conditions. I never actually get an error, it just won't finish even after 10 hours when it should take around 30 seconds to run. I am experimenting with different spatial and time node intervals, but it is hard, because I don't get feedback.
Is there some sort of equivalent to diagnostic for fsolve? stats is not useful because it only displays an output after fsolve is finished.
Check out the documentation on odeset, and specifically the stats option. I think you basically just want to set stats to on and you will get some feedback.
Also, depending on your ODE, you may need a different solver. About half way down the page on this page there is a list of most of the solvers available in MATLAB. Depending on whether your function is stiff or non-stiff, and how accurate you need to get, one of those might work better for you. Sometimes I just code them all in and comment out all but one until I find the one that runs the best for me, but check out the documentation on each if you want to find the "right" one for your application.
Your question is confusing because you refer to both ode15s and fsolve locking up. These are two completely different functions. One does numerical integration and the other solves for roots. Also, fsolve has no option called 'Stats' (see doc fsolve). If you want continuous output from fsolve use:
options = optimist('Display','iter');
[x,fval,exitflag] = fsolve(myfun,x0,options)
This will display the iteration count, number of function evaluations, the function value, and other stuff depending on what algorithm you use (the alorithm can be adjusted via the 'Algorithm' option). Again see doc fsolve for full details.
As far as the 'Stats' option with ode15s goes, it's not going to give you very much information. I doubt that it will you figure out why your system is halting (if it even is ode15s that you have a problem with). What you can try is using an output function via the 'OutputFcn' option of odeset. You can try the simple odeprint first:
options = odeset('OutputFcn',#odeprint)
which will print your state after each integration step. Type edit odeprint to see the code and how you might write your own output function if you need to do more.