Ubuntu – Matlab .desktop file

desktopMATLAB

(This is not about creating a launcher in the dock or anything like that.)

When I navigate into a folder with some Matlab .m files, and simply do

matlab myscript.m

in the terminal, it launches Matlab with working directory set to where the file is.

This has some perks: If there is also a startup.m file in the directory – and therefore in Matlab path – Matlab will notice and run it.

I tried to replicate this functionality with a .desktop file, so I could click an .m file (recognized as objective C by Ubuntu…) with the same results as if I run it from terminal, but no luck.

Here is what I have tried:

I put text/x-objcsrc=Matlab.desktop in /home/jakub/.local/share/applications/mimeapps.list

and created this desktop file in /home/jakub/.local/share/applications

Version=1.0
Type=Application
Terminal=false
Exec=matlab -desktop
Name=Matlab
Icon=/home/jakub/Pictures/icons/Apps-Matlab-icon.png
Categories=Development

When I click an .m file, it launches Matlab, but it is not equivalent to running it from the terminal (see above): Mainly, work dir is set to Matlab's default, instead of where the script is, and the script does not open, just plain Matlab with empty editor.

I also tried this:

Exec=matlab -desktop -r "edit %f"

which opens the file but does not set the work dir

Exec=matlab -desktop -r "cd %d"

which changes the work dir, but after Matlab is run, which means it will not run the code in startup.m (so it is not equivalent to doing matlab myscript.m in the terminal).

Is there any way of perfectly mimicking matlab myscript.m as run from the terminal, but in a desktop file?

Best Answer

I had the a similar issue, when I tried to set Matlab up manually.

Eventually, I ran:
sudo apt-get install matlab-support
and followed installation instructions. This just adds Matlab to the Dashboard.

The resulting matlab.desktop file reads:

[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Exec=matlab -desktop
Name=MATLAB
Icon=/usr/share/icons/hicolor/48x48/apps/matlab.png
Categories=Development;Math;Science
Comment=Scientific computing environment
StartupNotify=true
StartupWMClass=com-mathworks-util-PostVMInit

To open .m files in matlab by clicking on them I used the Ubuntu GUI to set it to open those files with the command:

matlab -desktop -r "open %U;"

This both opens the file in Matlab and sets the current working directory to where the file is located (at least with Matlab 2016b).

I hope this information helps.

Related Question