matlab

start matlab script from linux command line

If you want to start a matlab script from terminal (in linux or mac), you can use following command

matlab -nojvm -nodisplay -nosplash -r "addpath ('/path/to/script'), test_script(imgSize,'savepath'), exit"

Then matlab starts, executes the script and exits again.

test_script looks something like

function test_script(j,savepath)
img = rand(imgSize,imgSize);
imwrite(img, sprintf('%s/myTestImg.png',savepath),'png');
return

show no figures in matlab, disable figures

If you want to run a matlab script, that shows figures, on a remote machine without using ssh -X, start matlab with

matlab -nodesktop

and write following line in the matlab terminal, before starting your .m script

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

That prevents figures to pop up. To set the visibility on 'on', use

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

matlab - How to use a struct in a mex file

It took me some time until I figured out how to pass a struct to a mex file and especially how to get out the values.

To get the values from the struct following lines are important:

 /* get the values from the struct */
  const mxArray  *mxTmp; 
  double         *tmp;
 
  mxTmp = mxGetField(prhs[1],0,"par1");
  tmp   = mxGetPr(mxTmp); 

Here the full code

matlab do not show any figures

Sometimes you don't want to display figures or plots within matlab, e.g. if running matlab with the -nodesktop flag on a remote machine, e.g.

matlab -nodesktop

Include following lines in your code or paste it in the matlab command window to disable the figures:

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

Sometimes it is also good to include

Save Matlab Figures to pdf / convert eps files to pdf

In Matlab saving  a figure as a pdf results in a rather bad quality. It is better to save the figure as .eps and convert it to .pdf, e.g. in Linux via the terminal tool

epstopdf File.eps 

NOTE, especially if the .eps file is in landscape orientation, the terminal tool ps2pdf does not work properly and cuts off pdfs in portrait orientation.