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.,
sudo ln -s /path_to_MATLAB_installation/matlab
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
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!