Opening .m files DIRECTLY in MATLAB - matlab

I am using Ubuntu 10.04 and have MATLAB installed on my computer. I wanted to know if there was any way to open .m file directly in MATLAB without having to go through the painful process of starting MATLAB, and navigating through folders to open up desired scripts.

Try changing file associations for .m files so that you can double click on them and have them open in the MATLAB editor.

A startup file for user-defined options may fit your needs. Or have a look at shortcuts by Mike.

To open an m-file from matlab w/o navigating through menus:
>>edit myfile
The extension .m is assumed. This requires that myfile.m is in matlab's search path.
To add your favorite locations to matlab's search path, try
>>pathtool
and choose SAVE so the locations are preserved for your next Matlab session.

What worked for me is appending %F to the end of Exec=some/path which is in my matlab.desktop file.
Locate the matlab.desktop file its either in ~/.local/share/applications or ~/usr/share/applications.
Solution from here
Also if you don't have a matlab.desktop file then you may want to create one link to some relevant help to create launcher icon

Related

Calling user defined function anywhere? (matlab)

Where can i save my document "function.m", so i can call this function from any code in any path of my pc?
I already made some custom functions, but i dont know where to save those codes to be able to call them from any part.
Save them in a logical place where you can find them back. Then, use addpath to add that folder to MATLAB's search path, i.e. when you use a function it will try that folder as well to find it.
You can do this either per script, or edit startup.m with this, so that it's automatically included when MATLAB starts.
Alternatively to Adriaan's best practice answer, the fastest option is to save your function.m in the %USERPROFILE%/Documents/MATLAB directory on your PC ($home/Documents/MATLAB on Linux and Mac). This directory is on your MATLAB Search Path by default. This might be an acceptable solution if you use the function commonly across different projects.
Finally, there is a "manual" solution: Execute pathtool in MATLAB, add the directory containing your function.m via the GUI and hit save.

How to lock MATLAB files keeping them executable

How to lock MATLAB files keeping them executable.
I am writing code to create GUI where third party should run the .fig file but should be unable to read the code written. Main GUI editor file contains user defined functions also, where they should be locked too with capability of execution.
Thank you in advance.
You can p-code your MATLAB functions:
http://www.mathworks.co.uk/help/matlab/matlab_prog/protect-your-source-code.html

Double-clicking to open a file on a standalone matlab application

I have timeseries files with their own extension (.Z4R). I have compile my matlab GUI that read them.
Right now I load these files from the GUI (look for the path...). I wondering if it's possible to just double-click on the .Z4R in order to load it into my program.
Thank you.
I understand the window part.
Where I don't know it is about this command line that accept the input (Z4D) when the GUI .exe starts. How do I know the path of the double-clicked file.
Thanks
Marc
You should be able to do this via Windows file association (assuming you are running windows).
You'll need to check the command line arguments of your app and open supplied Z4R files as appropriate.
Have a look on google to see how to set-up file associations
If your compiled Matlab GUI was created using the Matlab tool guide, you should have a function
function YourProgramName_OpeningFcn(hObject, eventdata, handles, varargin)
varargin should contain the filename you double clicked on.

Does matlab have a matlabrc file?

Today I stumbled upon this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/112560
The question is basically how to make Matlab read your startup.m file regardless of where
you start your matlab session.
One of the solutions offered was:
One solution would be to ask the system administrator to add a few lines
to "matlabrc.m" that adds some pre-determined folder in the user's home
directory to the MATLAB path (say, ~/.matlabstart). Then each user can
have their own "startup.m" file inside this folder.
What I ended up doing in my system (OS X) was to add a startup.m file in:
/Applications/MATLAB_R2011a.app/toolbox/local/
In this startup.m file I added:
if exist([getenv('HOME') '/.matlabrc/startup.m'])
run([getenv('HOME') '/.matlabrc/startup.m']);
end
That way users have the option of creating the hidden folder ~/.matlabrc and inside it they can put the file startup.m. In this startup file they can tell matlab what to execute whenever they start Matlab regardless of the directory where they started it. An example of what I added to my own personal startup.m file is
addpath(genpath('/Users/jmlopez/matlabcode/'))
Now I can add as many folders inside that directory and all of them will be added
to the path every time I start Matlab automatically without having to modify the path.
The question is: Did Matlab already provided a special file like the one I created or did I just go through all this trouble to accomplish what I wanted? If the answer is the second option I gave, then, why doesn't Matlab provide this? It is such a pain in the ass to add directories to the Matlab path whenever you do not have admin permissions and I do not want to carry my startup.m file to every directory I go to. Can someone shed some light into this please?
You can save the pathdef file (which stores all the paths you add) to a custom directory. The problem however is that when matlab starts, it doesn't automatically know which custom directory you used in the previous session.
But that's where the MATLABPATH environment variable comes in. Because this allows to set the matlab starting path yourself. In linux this is simply done by setting this environment variable MATLABPATH before starting matlab (from a terminal / in your .bashrc / ...)
export MATLABPATH=$HOME/.matlab
This way you can let all users have their own pathdef file, which solves the problem of having to add them manually at startup.
EDIT
I tested out if adding startup.m to that MATLABPATH directory worked, ie: does matlab run that startup file? ... and it does. I think it doesn't work for you, because there is another startup.m file in some other (higher priority) directory (probably matlabroot), so that gets precedence. My only startup file is in MATLABPATH, so there is only one choice.
EDIT2
Nope, I added startup to matlabroot directory, and still my own startup file in .matlab gets run. Are you sure you set the MATLABPATH correctly before you started matlab?

MATLAB error when running the program "try.m"

After creating a MATLAB (version 2010a) file in the editor, I am getting the below mentioned error:
"MATLAB cannot run this file because \toolbox\matlab\lang\try.m shadows it in MATLAB path"
Also with this I get an option to "change folder", "add the path". Even after clicking and working around with this option I keep getting the same message.
I looked up in the matlab manual and tried to change the path. Also I checked my path variable, the place where I am storing my matlab files is already there in the path variable. Please help correcting the error. The name of my file is try.m
The code inside 'try.m' is just:
clear all;
TRY is a reserved word in MATLAB, so you shouldn't use it for your filename. Rename "try.m" to something else and you should be fine. You can do this using the "Save As" from the Editor menu and typing a different name.
Is your file in the file editor also named lang.m? Because Matlab sometimes cannot resolve scope differences between m-files in the current directory and m-files that represent built-in functions. It does not appear to be telling you that your file is not in the path; rather it seems to be saying it cannot distinguish your file from \toolbox\matlab\lang.
Can you provide more details about what your file is, such as posting the code and including the working directory name?