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'
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.
> 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.
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.
> 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.
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
> 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
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
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.
In article <0c3dc798-a65c-4dab-862f-dd8141aca...@q12g2000yqg.googlegroups.com>,
ImageAnalyst <imageanal...@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.
> 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
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.)
> 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.)
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.
> 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.
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);
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?