MATLAB: Run m-file From Linux Terminal / DOS

Yes, MATLAB m-files can very much be executed without initiating the MATLAB GUI. This can be quite handy when when running multiple codes simultaneously without creating a huge clutter for the user, or when running multiple instances of MATLAB and there is a need to prevent unnecessary eating up of the RAM through its GUI.

In Linux, it works via the re-direction operator ( < ), while in Windows, it has to be done using the -r flag. The steps are detailed below:

For MATLAB installations on Linux, ensure the MATLAB soft link is available in /usr/local/bin, so that it can be called from the terminal without specifying its full installation path, i.e.,

cd /usr/local/bin

sudo ln -s /path_to_MATLAB_installation/matlab

cd

matlab -desktop

Once MATLAB link has been created, exit the MATLAB GUI. Now run your m-file (say: test_run.m) from the terminal as:

matlab -nodesktop -nosplash < /path_to_file_location/test_run -logfile test_run.log

Thats it! The “-nodesktop” option ensures the full GUI isnt initiated and only the MATLAB command prompt pops up, “-nosplash” prevents the MATLAB splash screen from showing up. The redirection operator will run all the commands in the m-file as they would in a normal way and “-logfile” logs all that shows up in the MATLAB command window.

In Windows, the slight modification is that we have to first cd to the location where the m-file is present

cd path_to_file_location\

matlab.exe -nodesktop -nosplash -r test_run -logfile test_run.log

And we are done!

P.S : 1) Dont forget to check out the difference in RAM usage with “-nodesktop” enabled and normal GUI way

2) DO NOT put .m with the file name when using this command, else it’ll result in an error!

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s