Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to change directoy using matlab CD to the folder that one of my m file is opend from?

176 views
Skip to first unread message

Nasser M. Abbasi

unread,
Feb 13, 2012, 5:28:33 PM2/13/12
to
I have an m file open in the editor, and it lives in say folder_A.

is there a way from Matlab to tell it:

please cd (make current folder) to the folder that this m
file open in the editor is located in? If there is more
than one m file open in the editor, I should be able
to select one

ofcourse, I can just type the command

cd folder_A

But the path is too long, and Matlab does not seem to allow me
to copy the path shown at the top of the window banner so I
can paste it on the console window.

What would be nice, if from the editor itself, I can right-click
on the file window, and have a pop-up window open with an option
to 'cd to the folder this file is in'

thanks,

--Nasser

ImageAnalyst

unread,
Feb 13, 2012, 5:52:56 PM2/13/12
to
I agree. Once Brett Shoelson at the Mathworks told me to put this at
the start of my code:
% Change the current folder to the folder of this m-file.
if(~isdeployed)
cd(fileparts(which(mfilename)));
end

I think it maybe used to work but now it doesn't. It asks me to
change directory, or add to path before the m-file even runs and gets
to that code. I've put in a request to have an option to do some kind
of an auto-switch to the folder where your m-file lives when you try
to run it but I don't think they liked that idea, so we are stuck with
the annoying prompts all the time. It's especially bad for me because
I work on several different projects that live in different folders so
I'm always having to answer the prompt when an auto-switch option
would obviate that.

I'm not sure your suggest solution is any better than the current
situation of just running the file and then clicking "change folder"
when the popup comes.

Nasser M. Abbasi

unread,
Feb 13, 2012, 6:33:31 PM2/13/12
to
On 2/13/2012 4:52 PM, ImageAnalyst wrote:
> I agree. Once Brett Shoelson at the Mathworks told me to put this at
> the start of my code:
> % Change the current folder to the folder of this m-file.
> if(~isdeployed)
> cd(fileparts(which(mfilename)));
> end
>
> I think it maybe used to work but now it doesn't. It asks me to
> change directory, or add to path before the m-file even runs and gets
> to that code.

When I add the above code, it did CD, after I run the function,
and it did NOT ask me anything. after I run it, I found that
my folder was changed from the one I started from, which
is C:\Users\me\Documents\MATLAB

However, this solution means I have to 'run' a function, just
to cd to the folder it is in. What If I do not want to run it?
I just wanted to cd to that folder first, may be to check that
I have some data files in there, or for some other reason.

You solution helps, but I think Mathworks needs to come up
with a designed solution build-into the editor system itself.

> I've put in a request to have an option to do some kind
> of an auto-switch to the folder where your m-file lives when you try
> to run it but I don't think they liked that idea, so we are stuck with
> the annoying prompts all the time.

Again, I did not get the prompt. I am using 2010a on windows.

>
> I'm not sure your suggest solution is any better than the current
> situation of just running the file and then clicking "change folder"
> when the popup comes.

Except my solution would not require one to run the function first
in order to cd to the folder the function is located in.

regards,
--Nasser

ImageAnalyst

unread,
Feb 13, 2012, 8:18:13 PM2/13/12
to
Right. I know and agree with everything.

The prompt I was talking about is the one where you have folder1 as
your Current Folder, and you're trying to runmfile2.m which is located
in folder2 but open in the editor. When you click the green play
triangle, you should get a popup that says "File blah.m is not found
in the current folder or on the MATLAB path. To run this file, you
can either change the MATLAB current folder or add its folder to the
MATLAB path." and then it has 4 buttons that say "Change Folder" "Add
to Path" "Cancel" and "Help". I'm not sure why you don't get such a
popup message. I know it's been there for many versions.

Nasser M. Abbasi

unread,
Feb 13, 2012, 8:30:25 PM2/13/12
to
I do not know. But to be clear, this is the setup I have:

foo.m is in G:\folder_A
folder_A is _allready_ on my matlab path.

I start Matlab. current folder is C:\Users\me\Documents\MATLAB

I'd like my current folder to be G:\folder_A. No way to do it easily from
Matlab. Even though in the editor I have the file foo.m open, since
I had it open before I closed Matlab last time.

I put your code

if(~isdeployed)
cd(fileparts(which(mfilename)));
end

in my foo.m, then I clicked on the editor's green arrow and run
foo.m, Matlab did NOT ask me if I want to change folder.

When the function completed, I found that the current folder was
G:\folder_A

So, no prompt.

Matlab 2010a on windows 7.

regards
--Nasser

Steven_Lord

unread,
Feb 13, 2012, 10:26:43 PM2/13/12
to


"Nasser M. Abbasi" <n...@12000.org> wrote in message
news:jhc2qd$bfq$1...@speranza.aioe.org...
> I have an m file open in the editor, and it lives in say folder_A.
>
> is there a way from Matlab to tell it:
>
> please cd (make current folder) to the folder that this m
> file open in the editor is located in? If there is more
> than one m file open in the editor, I should be able
> to select one

Write a shortcut that uses the Editor API.

http://blogs.mathworks.com/desktop/2011/05/09/r2011a-matlab-editor-api/

The body of the shortcut should be:

fileItem = matlab.desktop.editor.getActive;
cd(fileparts(fileItem.Filename));

Add error handling to taste. Now you just need to select the appropriate
file and press the shortcut button. [Of course, if you want to do this
programmatically, you could turn this into a function.]

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

ImageAnalyst

unread,
Feb 13, 2012, 10:33:24 PM2/13/12
to
Right, and maybe that's what Brett was thinking. It works if folder_A
is already on the path, because it will run the m-file in that case.
However if folder_A is not already on the path, you'll get the popup
like I said.

My problem is that I have lots of files with the same name but slight
modifications, like movemask.m. I may have 5 versions of that, just
slightly different. Sometimes it seems it would run the wrong one so
I've decided to not put all 100 of my MATLAB project folders on the
path, just ones that really have utilities I need, like the Utilities
folder, etc. So if I try to run a file in the editor, and that file
does not live in the current folder, I'll get the popup before it runs
a single line of code, Brett's code notwithstanding. If that folder
were on the path, I would not get the popup.

Doug Schwarz

unread,
Feb 13, 2012, 10:37:47 PM2/13/12
to
In article
<0c3dc798-a65c-4dab...@q12g2000yqg.googlegroups.com>,
ImageAnalyst <imagea...@mailinator.com> wrote:

> I agree. Once Brett Shoelson at the Mathworks told me to put this at
> the start of my code:
> % Change the current folder to the folder of this m-file.
> if(~isdeployed)
> cd(fileparts(which(mfilename)));
> end

Inside a function

cd(fileparts(mfilename('fullpath')))

will do what you want.


> I think it maybe used to work but now it doesn't. It asks me to
> change directory, or add to path before the m-file even runs and gets
> to that code. I've put in a request to have an option to do some kind
> of an auto-switch to the folder where your m-file lives when you try
> to run it but I don't think they liked that idea, so we are stuck with
> the annoying prompts all the time. It's especially bad for me because
> I work on several different projects that live in different folders so
> I'm always having to answer the prompt when an auto-switch option
> would obviate that.

In my opinion, a function should never run cd at all. The reason is
that when the function fails it will leave the user "stranded" in some
folder other than the one he/she was in when the function was run. Yes,
you can write code to return to the original folder in the event of an
error, but it's simpler just to refer to files by their full paths
inside the function.

Also, it's best to put every folder containing m-files on the path and
data elsewhere. I am constantly trying to convince my co-workers to do
this, but they insist on having different sets of data in different
folders with slightly different versions of their program *all with the
same name* residing in said folders. It confuses them, but they insist
on doing things that way.

I feel strongly that MATLAB should not automatically change the current
folder EVER.

--
Doug Schwarz
dmschwarz&ieee,org
Make obvious changes to get real email address.

Doug Schwarz

unread,
Feb 13, 2012, 10:47:03 PM2/13/12
to
In article <jhc2qd$bfq$1...@speranza.aioe.org>,
How about a simple function you can run to cd to the folder of the
active edit window m-file? Just put this function on your path and type
"cde" when you want this. (Works in R2011a.)

--------- cde.m --------------
function cde
current_edit = matlab.desktop.editor.getActive();
the_dir = fileparts(current_edit.Filename);
cd(the_dir)

Nasser M. Abbasi

unread,
Feb 13, 2012, 11:32:39 PM2/13/12
to
On 2/13/2012 9:47 PM, Doug Schwarz wrote:

>
> How about a simple function you can run to cd to the folder of the
> active edit window m-file? Just put this function on your path and type
> "cde" when you want this. (Works in R2011a.)
>
> --------- cde.m --------------
> function cde
> current_edit = matlab.desktop.editor.getActive();
> the_dir = fileparts(current_edit.Filename);
> cd(the_dir)
>

Very nice Doug!

Yes, this works very well. I allready did that. Very easy to use.

Thank you, and thanks for other suggestion by Steve also.

--Nasser

ImageAnalyst

unread,
Feb 14, 2012, 8:59:40 PM2/14/12
to
On Feb 13, 10:26 pm, "Steven_Lord" <sl...@mathworks.com> wrote:
> fileItem = matlab.desktop.editor.getActive;
> cd(fileparts(fileItem.Filename));

> --
> Steve Lord
--------------------------------------------------------------
But Steve and Doug's solutions don't save me any clicks. I either
click their shortcut button and run my m-file, or I run my m-file and
then click "Change Folder" - two clicks either way.

Steven_Lord

unread,
Feb 15, 2012, 10:19:49 AM2/15/12
to


"ImageAnalyst" <imagea...@mailinator.com> wrote in message
news:fd120401-7638-4fcc...@l14g2000vbe.googlegroups.com...
Try this:


function varargout = callOpenDocument(varargin)
% If the function open in the Editor is a function that accepts 2 inputs and
returns 3 outputs,
% call callOpenDocument like:
%
% [a, b, c] = callOpenDocument(5, 27);

fileItem = matlab.desktop.editor.getActive;
[thepath, thename, theext] = fileparts(fileItem.Filename);
cd(thepath);
fh = str2func(thename);
[varargout{1:nargout}] = fh(varargin{:});


You could add in error checking or CD back to where you came from after
calling the function.

Personally, I'd probably just put the directory containing that file on the
MATLAB search path if that were an option :)

ImageAnalyst

unread,
Feb 15, 2012, 4:06:43 PM2/15/12
to
On Feb 15, 10:19 am, "Steven_Lord" <sl...@mathworks.com> wrote:
> function varargout = callOpenDocument(varargin)
[snip]
> Steve Lord
> sl...@mathworks.com
> To contact Technical Support use the Contact Us link onhttp://www.mathworks.com

---------------------------------------
Where would that go? And how would it avoid a click?
0 new messages