Clear Filters
Clear Filters

Opening .m Files on Linux

57 views (last 30 days)
Nicholas on 16 Mar 2024
Edited: JH Schue on 3 May 2024
I am running Matlab 2021b with Ubuntu 20.04.3. I would like to be able to doubleclick .m files so that they automatically open in Matlab. The main problem is that right-clicking the file -> Properties -> Open With doesn't show Matlab as an application.

Answers (1)

Hassaan on 17 Mar 2024
1. Create a MATLAB .desktop File
First, check if a MATLAB .desktop file already exists in /usr/share/applications/ or ~/.local/share/applications/. If it doesn't, you'll need to create one.
  1. Open a terminal (Ctrl+Alt+T).
  2. Create a new .desktop file for MATLAB. Replace <MATLAB_VERSION> with your MATLAB version (e.g., R2021b) and <YOUR_USERNAME> with your actual username.
nano ~/.local/share/applications/matlab.desktop
Add the following content to the file. Adjust the paths if your MATLAB installation is in a different location.
[Desktop Entry]
Exec=/usr/local/MATLAB/<MATLAB_VERSION>/bin/matlab -desktop
Comment=Start MATLAB - The Language of Technical Computing
Save and close the editor (Ctrl+O, Enter, Ctrl+X).
2. Associate .m Files with MATLAB
  1. Still in the terminal, set MATLAB as the default application for .m files using the mime-type association.
mimeopen -d ~/example.m
You'll see a prompt asking you to choose an application. If MATLAB is listed, select it by typing its number. If it's not listed, select the option to use a custom command, and enter the command you used in the Exec field of the MATLAB .desktop file.
3. Check the Association
Double-click an .m file in your file manager to check if it now opens in MATLAB.
  • If MATLAB still doesn't show up in the "Open With" list, try logging out and logging back in, or rebooting your system.
  • Ensure the .desktop file's Exec path correctly points to your MATLAB installation, and that the command works when entered in the terminal.
  • If double-clicking opens MATLAB but not the specific file, MATLAB's desktop startup options may need to be adjusted or it could be a limitation of how MATLAB handles file opening arguments from the desktop environment.
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
JH Schue
JH Schue on 3 May 2024
btw, change
Exec=matlab -desktop
Exec=matlab -desktop -r "edit %f"
so it won't open an empty matlab editor
JH Schue
JH Schue on 3 May 2024
Edited: JH Schue on 3 May 2024
If you have executed 'mimeopen -d ~/example.m', then Open With will show an application called 'usr', this is the true matlab editor... However, 'example.m' can not cover all .m file, now you can just right click any .m file and choose to Open With usr.
I have a matlab icon in Open With, but it never works, so I think it's just a bug of matlab. It points to a wrong path

Sign in to comment.


Find more on Startup and Shutdown in Help Center and File Exchange





Community Treasure Hunt

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

Start Hunting!