Set MATLAB as default for .m files in Ubuntu without opening an empty editor

According to the previous thread, MATLAB can be made to open .m files by default in Ubuntu by editing the matlab.desktop file (usually in /usr/share/applications) and changing:
Exec=matlab -desktop
to
Exec=matlab -desktop -r "edit %f"
This has the side effect that opening MATLAB from its icon causes an empty editor window to be open. Can anyone advise how to avoid this? It looks like using X-Ayatana-Desktop-Shortcuts may help, but I cannot figure out how to make just opening the command window the default and opening the editor to happen only if a file is specified.

Answers (1)

Old thread, but in case anybody finds this, I solved this on Linux Mint 22.3 by creating a custom bash script that runs a different command based on if there's a file specified or not.
1) Create a bash script called run_matlab.sh and paste this into it:
if [ $# -gt 0 ]; then
/usr/local/MATLAB/<your-matlab-version>/bin/matlab -desktop -r "edit('$1')"
else
/usr/local/MATLAB/<your-matlab-version>/bin/matlab -desktop
fi
2) Make your bash script executable by running this command into the terminal:
sudo chmod +x <path-to-file>/run_matlab.sh
3) Edit your .desktop file to run your bash script:
Exec=<path-to-file>/run_matlab.sh %f
P.S. If you also want to change your matlab directory to the file that you open, you can add this to the top of your bash script:
dirName=$(dirname "$1")
cd "$dirName"

Categories

Products

Release

R2021a

Asked:

on 6 Apr 2021

Answered:

on 11 Apr 2026 at 6:53

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!