"Open With" Matlab—How-To for Linux

This post explains how to set .m (matlab source code files) to open with Matlab (a how-to for Gnome Linux).
  1. Right-click a .m file and click Properties
  2. Click the Open With tab then click Add
  3. Expand Use custom command and type the following into the field:
matlab -desktop -r "f=%f; cd(fileparts(f)); edit(f); clear f;"
  1. Click Add then select the radio button next to matlab.
  2. Close the property window.
Now all of your .m files should open with Matlab. The only thing I can't fix is that a new instance of Matlab is opened every time you open a .m file. To help, I set the current directory in Matlab to be that of the .m file which is being opened. Now you should be able to open your other .m files in Matlab more easily, without opening multiple instances.

If you'd like to know what that command does:

matlab: to run Matlab
-desktop: to run Matlab without using a terminal
-r: execute following matlab code when Matlab opens
f=%f: Save filename of file to open to variable f
cd(fileparts(f)): change the current directory to file f's directory
edit(f): open file f for editing
clear f: delete the matlab variable f

6 comments:

Anonymous said...

It is a pity, that now I can not express - I am late for a meeting. I will return - I will necessarily express the opinion on this question.

Anonymous said...

And it is effective?

Unknown said...

It does work... but sometimes linux confuses other text files for matlab files, so they open in matlab, too. This is very annoying.

Anonymous said...

This could be useful to the ubuntu package "matlab-support" and to the rest of the community.
Would you mind writing to the mantainer, or if I did it?

Anonymous said...

This could be useful to the ubuntu package "matlab-support" and to the rest of the community.
Would you mind writing to the mantainer, or if I did it?

Richard Coombs said...

@Poomerang: Not at all, that's a great idea! I use that package myself. I don't have the contact details, but please go ahead and contact the developer. Use my code as you wish :-)