Does matlab have a matlabrc file?
Asked Answered
N

1

8

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?

Nogging answered 16/6, 2012 at 2:59 Comment(1)
If you are going to use words that some might find offensive, use them. If are concerned not to give offense to the sensitive, don't pretend that a$$ hides your ass. I've edited your question to give you the courage of my convictions.Poree
A
6

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?

Adriene answered 16/6, 2012 at 17:37 Comment(6)
I saw that folder, but I could not find anywhere where it says that that is where you can save your pathdef file. In any case, I rather not mess with the pathdef.m file since that would require me to explicitly state the paths. I rather just have a startup.m file with the commands addpath(genpath('path/to/directory')). In that way if I modify the structure of the directory I won't have to worry about adding all the subdirectories to the path. Can I just put a startup.m file in $HOME/.matlab? I'm about to find out.Nogging
That did not work. I knew about the MATLABPATH because of the docs. But that would only work in Mac/Linux, and it doesn't mention that I can put my pathdef file there. In any case, for now the only way I see it possible is to have an admin add that if statement so that a user can define his startup.m and put whatever they feel like there. I tried making it display 'Hello jmlopez, you are dumb for not knowing about me' in the startup.m file inside $HOME/.matlab but sadly it didn't display it.Nogging
I tested the same thing out, and it does work for me. see my edit ^^Adriene
So it seems that Matlab executes every startup.m file that its in path when it starts. Right? Yeah, it does say so here I did add $HOME/.matlab to the MATLABPATH as you have said, so now since I have a startup.m file there it executes it. This works well, :). Now, one last question, How would this apply to a windows machine? Is there an environment variable that we can modify in windows? Anyway, I'll accept your answer because you made my confusion go away.Nogging
it only executes the first one it encounters on its search path (like any other script you ever try to run with matlab)!! For windows it is explained here that you can do it simply by creating a shortcut for matlab. The directory where the shortcut is in, will be the MATLABPATH.Adriene
You are indeed correct. If I create a startup.m file in a folder and I start matlab from there then the startup.m file in my $HOME/.matlab won't get executed. Got it. Good luck windows users.Nogging

© 2022 - 2024 — McMap. All rights reserved.