Associating the MATLAB Editor with .m files in GNOME

November 13th, 2008 | Categories: Linux, matlab | Tags:

If you double click on a MATLAB .m file on a Windows machine then it will open the file in the MATLAB editor by default.  So how do you get similar behaviour in Linux you might ask?  This question was recently posed on the MATLAB users group and since no one gave an answer I thought I would have a go and see what I could come up with.  After all, how hard could it be?

I’ll cut a long story short and just tell you the solution.  To be specific the following tip works on Ubuntu 8.10 running GNOME 2.24.1 but I imagine that a similar procedure would work for some other setups as well.

  • Right click on any .m file you like and, in the menu, click on Open With->Open With Other Application
  • Click on the arrow to the left of Use a custom command
  • In the resulting dialogue box type


matlab -desktop -r "edit %f"

  • Click on Open

MATLAB will start up and then the editor will open your file. In future you will not need to type the above command because GNOME will have added an extra item to the Open With menu – namely Open with “matlab”

Hope this helps someone out there and, as always, comments are welcome.

If you enjoyed this article, feel free to click here to subscribe to my RSS Feed.

  1. paul
    November 14th, 2008 at 02:11
    Reply | Quote | #1

    Hi,

    I was the person with the original question on the Matlab users forum.

    The above solution works just great.

    However, I had to change
    matlab
    to
    /opt/matlab/bin/matlab
    which is where my executable was located/

    Now if I can only understand all the bits and pieces of the command!

  2. paul
    November 14th, 2008 at 02:17
    Reply | Quote | #2

    I forgot to ask in my previous post:

    how do I remove the old “matlab” entries from the “open with” menu? These are attempts that didn’t work but the link is still there. Now I have 4 entries in the “open with” called “matlab” but only one of them works.

  3. Mike Croucher
    November 15th, 2008 at 15:53
    Reply | Quote | #3

    Hi Paul

    The reason you had to put in /opt/matlab/bin/matlab rather than just matlab is because you don’t have matlab included in your path for some resason.

    To remove the old entries try the following:

    Open a terminal and type

    cd .local/share/applications/
    ls

    From the ‘ls’ command you should see several files that look something like userapp-matlab-HL7NKU.desktop although the exact name may vary. Delete all of the userapp-matlab* files

    Now open a file called mimeapps.list using gedit:

    gedit ./mimeapps.list

    Delete the line that starts ‘text/x-matlab=’, save the file and close gedit.

    This procedure will remove all associations with MATLAB so you will need to redo the steps I talked about in my original post.

  4. wayne
    November 18th, 2008 at 15:09
    Reply | Quote | #4

    Hi,Mike

    Wayne coming again,

    i did curiously as you told to associate .nb files with Mathematica , but Mathematica started with a “why the beep?” messagebox, and the result is an empty notebook , :(.
    the error message is
    “Mathematica was unable to open the file edit’/home/tod/PursuitCurves-source.nb’.You may have a problem with your disk,or if you are using a fileserver,someone else may be using the file.Error code=-43.”

    where problem lies?

    and by the way ,i couldnt find mimeapps.list in /usr/local/share/applications/ to get rid of the invalid
    userapp** texts.
    did i missed something? thank you , hope My second language had made myself understood

  5. Mike Croucher
    November 18th, 2008 at 15:25
    Reply | Quote | #5

    Hi Wayne

    Sorry – I assumed that your terminal would open inside your home directory so the cd command I posted is relative to that. On my machine my home directory is /home/mike so the full path to mimeapps.list is

    /home/mike/.local/share/applications/mimeapps.list

    of course yours will be relative to your home directory (also note that the correct directory is .local NOT local)

    Let me know if that helps.

    As for mathematica – it should just work if you simply put the full path to mathematica in the ‘open with’ box. For example on my machine I simply put

    /usr/local/bin/mathematica

    The “edit %f” applies ONLY to MATLAB and not to any other application.
    Hope this helps

  6. wayne
    November 19th, 2008 at 13:15
    Reply | Quote | #6

    it worked,
    sorry i forgot to tell you i taught myself linux,so in the previous post,i did not notice that the dot”.” is used to hide files.

    now i just deleted the whole files “mimeapps.list” and “mathematica”,then “open with other application”,simply put “mathematica”,because on my machine, “mathematica” is in one of my $PATH as the default.also worked.

    in fact,what i really desire is to let all .nb file have a spikey icon but not the seemingly text ico, it seems that in my ubuntu8.10 OS, all.nb files are previewed exactly the same with the .doc ,.chm files.
    can i expect them to behave the same as they do in windows OS ? :)

    thank you for taking time for my concern.

  7. David
    November 9th, 2009 at 16:16
    Reply | Quote | #7

    Hi Mike. Nice blog!

    I’ve a little problem with *.m files in Ubuntu 9.10. I’ve installed Mathematica 7 and now extensions *.nb, *.nbp and *.m are opened with Mathematica, because they are “Mathematica Notebook” type. I want to open *.m to with gedit (I don’t use matlab, I use octave), however if I change the properties in nautilus then all “Mathematica Notebook” extensions are changed and the *.nb files are now opened with gedit instead of Mathematica.

    Can you help me? I want to open the *.nb with Mathematica and *.m with gedit, how can I break the link between *.nb and *.m files?

    Sorry for my English, I’m Spanish.

  8. emorkay
    September 28th, 2010 at 00:00
    Reply | Quote | #8

    Hi,
    I’ve been looking for such a solution. But, this command opens up another editor window in a new Matlab instance. This should be opened with the same Matlab editor which is already opened. Can this be done in anyway?

    Thank yoou,

  9. fred2014
    December 21st, 2013 at 10:40
    Reply | Quote | #9

    that does not work with Ubuntu 13.10 why?

  10. alda
    August 14th, 2019 at 08:16

    Hey,
    Thanks for this, it worked very well in combination with the prescription here: https://askubuntu.com/questions/67382/add-custom-command-in-the-open-with-dialog
    as I am using ubuntu 18.04. But I would like to know how to open the figures without opening new instances of the matlab desktop/editor. I would like to be able to use a currently open matlab editor to open the file or not open an editor at all. Also, for anyone who might’ve also had this issue, when I used “edit %f” it opened a binary file in my MATLAB editor, the solution for me was to use “openfig %f” which opened an editor and the fig.
    Best

  11. alda
    August 14th, 2019 at 08:40

    Ok, so for anyone who is interested, I solved this by making a function I called opener in my default path MATLAB file:
    function opener(figure)
    openfig(figure)
    uiwait
    end
    Then I changed the executable command to be /path/matlab -nodesktop -nosplash -r “opener %f” and it opens in a uifigure application without an editor and it doesn’t immediately close! Hope this helps