MATLAB: Creating a desktop shortcut to run an mfile, Windows 7

defaultdesktopiconrunshortcutstartwindowswindows 7

Hey Everybody,
I have a couple questions about creating desktop shortcuts to run an mfile.
I would like to create a desktop shortcut that when clicked will run the mfile (not just open it). In addition to this, I would like to be able to distribute the mfile to others and still have the shortcut work. I am having lots of problems with getting the "relative path" required for the shortcut to work.
The two areas I need help with is:
1. (Most Important) Having other people be able to use my shortcut. Right now, I can put my shortcut anywhere on my computer and it will work fine. The problem is that when another person tries to use the shortcut, it points to the directory that was on my computer. I looked online extensively and realized I think this is really complex, maybe not with MATLAB, but at least with other directories. Ideally I think we just need to make a relative path from where the shortcut is located (this stays constant) but I think that is harder to do than you would initially think.
2. Assuming I can get the above working, I would like to set the default action of the shortcut to run the mfile, not just open it up in the editor. I am assuming this would involve a command line process, but I am open to any ideas or suggestions.
I have a feeling my ending solution will involve creating a shortcut to a .bat file which is OK, I am just not very familiar with DOS commands and what not so would probably need some extra guidance if that indeed is the solution.
Thank you in advance for your help!

Best Answer

  • A batch file to do this is almost exactly the same as a shortcut. You still need to know the MatLab install path. Now, I did a standard installation of MatLab, and I have found that it has put the MatLab bin directory into my path.
    Hit Windows-R, type cmd and hit Enter. Then type path and press Enter. You should see MatLab in there. If you want to exploit this, the caveat is that if there are multiple MatLab installations, you can't control which one it will run. But the bonus is you don't need to know which one they have. They probably only have one anyway.
    So, you should be able to create a batch file in the same directory as your script (call it myscript.bat), and put the following in:
    @echo off
    matlab -nosplash -r "myscript"
    Where myscript is the name of your script or function to run.
    You may wish to run "myscript; quit" -- I don't know what your purposes are.
    If your script has multiple subdirectories I can help you dynamically set the MatLab search path. Otherwise I'll forget that and keep this simple.
    The only trick now, is that a user might want a shortcut to this batch file, which they will have to do manually. Unless, I suppose, you wanted to create an installation script (which you could probably write in MatLab).....