Thread Subject: "Change directory" dialog box

Subject: "Change directory" dialog box

From: André Brito

Date: 7 Oct, 2009 15:57:02

Message: 1 of 15

Hello,

is there any way (other than adding a line of code in every file) to set the MATLAB preferences such that the default behavior for running m-files is to change the current directory to the directory where the file is located ?

The dialog that appear has the options "Change Directory", "Add to Path", "Cancel", and "Help".

Thanks,
Andr

Subject: "Change directory" dialog box

From: dpb

Date: 7 Oct, 2009 17:58:45

Message: 2 of 15

Andre Brito wrote:
> Hello,
>
> is there any way (other than adding a line of code in every file) to
> set the MATLAB preferences such that the default behavior for running
> m-files is to change the current directory to the directory where the
> file is located ?
...

automagically, how is it to know where the m-file is located unless the
directory containing the m-file is already in the search path or the cwd?

I don't see a way to do this other than by having the m-file make such a
change on its own which seems a poor idea.

In general, one organizes ML files into project-related directories and
makes a high-level startup file to set the proper parameters for a given
project that would include setting working directory, path for
project-specific files, etc.

OTOH, most users also create their subdirectories of custom functions
that are more generic that they want available between projects and use
the preferences files or startup to set the search path for such
directory(ies).

What, specifically, kind of an issue are you trying to solve--perhaps
more background would lead to other ideas/solutions others have had for
similar situations.

--

Subject: "Change directory" dialog box

From: ImageAnalyst

Date: 7 Oct, 2009 18:23:47

Message: 3 of 15

On Oct 7, 11:57 am, "Andre Brito" <andre_brit...@hotmail.com> wrote:
> Hello,
>
> is there any way (other than adding a line of code in every file) to set the MATLAB preferences such that the default behavior for running m-files is to change the current directory to the directory where the file is located ?
>
> The dialog that appear has the options "Change Directory", "Add to Path", "Cancel", and "Help".
>
> Thanks,
> Andr

---------------------------------------------------------------
No there is not. This is something I've brought up with them before,
and will reiterate when I meeti with their developers later this
month. It would be a big convenience. I keep hoping.

Subject: "Change directory" dialog box

From: André Brito

Date: 8 Oct, 2009 10:52:02

Message: 4 of 15

dpb <none@non.net> wrote in message <hail59$ojp$1@news.eternal-september.org>...
> automagically, how is it to know where the m-file is located unless the
> directory containing the m-file is already in the search path or the cwd?
>
> I don't see a way to do this other than by having the m-file make such a
> change on its own which seems a poor idea.
>
> In general, one organizes ML files into project-related directories and
> makes a high-level startup file to set the proper parameters for a given
> project that would include setting working directory, path for
> project-specific files, etc.
>
> OTOH, most users also create their subdirectories of custom functions
> that are more generic that they want available between projects and use
> the preferences files or startup to set the search path for such
> directory(ies).
>
> What, specifically, kind of an issue are you trying to solve--perhaps
> more background would lead to other ideas/solutions others have had for
> similar situations.

It's just a project where I'm constantly bouncing between directories at the same level. As there are other versions of the same files in other directories I can't add these working directories to the path, which would lead to a big mess, as you can imagine.

By the way, nice OTOH acronym!


ImageAnalyst <imageanalyst@mailinator.com> wrote in message <021bf50b-6fc8-40fe-978f-ae9f9e0b1a5d@l34g2000vba.googlegroups.com>...
> No there is not. This is something I've brought up with them before,
> and will reiterate when I meet with their developers later this
> month. It would be a big convenience. I keep hoping.

Wow, you get to have meetings with the developers?

Andr

Subject: "Change directory" dialog box

From: dpb

Date: 8 Oct, 2009 13:02:13

Message: 5 of 15

Andre Brito wrote:
...
> By the way, nice OTOH acronym!
...
Not of my making, certainly...

--

Subject: "Change directory" dialog box

From: Jan Simon

Date: 8 Oct, 2009 16:06:00

Message: 6 of 15

Dear Andre!

> is there any way (other than adding a line of code in every file) to set the MATLAB preferences such that the default behavior for running m-files is to change the current directory to the directory where the file is located ?

This behaviour would be really confusing!
E.g. if you call FOPEN, the current directory would be matlabroot\toolboxmatlab\iofun\ and a file without a path is searched there. If you call FOPEN(WHICH(FileName)), the file is searched in matlabroot\toolbox\matlab\general at first...
I think, the frequent changes when calling "SIN(a + ABS(b))" would slow down Matlab completely: The + should change the current folder to the location of "plus.m" --- except if [b] is an object with an own PLUS operator...

I have a simple tool for situations like yours: A dialog has a popup, which activates different path sets. In fact, even the GUI is not needed:
  % Use ADDPATH/RMPATH to create one path;
  Path1 = path;
  % Use ADDPATH/RMPATH to create other path;
  Path2 = path;
Now you can toggle the paths, e.g.:
  path(Path1)
This will fail, if the current directory contains some of the called M-Files. So just CD to a neutral location.

A GUI for this has the advantage, that even the debugger break points can be stored/restored automatically in the callback, if the directory is changed.

Kind regards, Jan

Subject: "Change directory" dialog box

From: ImageAnalyst

Date: 9 Oct, 2009 12:41:14

Message: 7 of 15

On Oct 8, 6:52 am, "Andre Brito" <andre_brit...@hotmail.com> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <021bf50b-6fc8-40fe-978f-ae9f9e0b1...@l34g2000vba.googlegroups.com>...
> > No there is not.  This is something I've brought up with them before,
> > and will reiterate when I meet with theirdeveloperslater this
> > month.  It would be a big convenience.  I keep hoping.
>
> Wow, you get to have meetings with thedevelopers?
>
> Andr-
----------------------------------------------------------------------------------------------------------------------------------------------------------
Andr:
It's not unusual (fortunately). I imagine many people who you see
frequently in this newsgroup also have. It's nice that The Mathworks
and other software companies (e.g. Microsoft, etc.) meet with heavy
users of their products to learn of their needs. It helps them design
and develop better products for the future that will help us all.
Since my job function is image analysis and I program in MATLAB every
hour at work (when I'm not doing email, organizing symposia, having
meetings, eating lunch, etc.), as well as many hours when not at work,
I guess that qualifies me. Of course they'll listen to any user if
you send a suggestion, comment, or bug report to
support@mathworks.com.
Regards,
ImageAnalyst

Subject: "Change directory" dialog box

From: André Brito

Date: 9 Oct, 2009 14:07:02

Message: 8 of 15

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hal2l8$25f$1@fred.mathworks.com>...
> Dear Andre!
>
> > is there any way (other than adding a line of code in every file) to set the MATLAB preferences such that the default behavior for running m-files is to change the current directory to the directory where the file is located ?
>
> This behaviour would be really confusing!
> E.g. if you call FOPEN, the current directory would be matlabroot\toolboxmatlab\iofun\ and a file without a path is searched there. If you call FOPEN(WHICH(FileName)), the file is searched in matlabroot\toolbox\matlab\general at first...
> I think, the frequent changes when calling "SIN(a + ABS(b))" would slow down Matlab completely: The + should change the current folder to the location of "plus.m" --- except if [b] is an object with an own PLUS operator...


Allow me to disagree: the pop-up dialog only shows up if you are trying to run an m-file which is located in a directory which is not included in the path. If you run FOPEN, for example, MATLAB knows where to search for it.

When the dialog shows up and when you press "Change directory", I assume that MATLAB adds this directory temporarily to the path and removes it in the end. This is the behavior I would like to be possible to be set as default in some check box in the MATLAB Preferences.

Andre

Subject: "Change directory" dialog box

From: ImageAnalyst

Date: 9 Oct, 2009 14:22:24

Message: 9 of 15

On Oct 9, 10:07 am, "Andre Brito" <andre_brit...@hotmail.com> wrote:
> "Jan Simon" <matlab.THIS_Y...@nMINUSsimon.de> wrote in message <hal2l8$25...@fred.mathworks.com>...
> > Dear Andre!
>
> > > is there any way (other than adding a line of code in every file) to set the MATLAB preferences such that the default behavior for running m-files is to change the current directory to the directory where the file is located ?
>
> > This behaviour would be really confusing!
> > E.g. if you call FOPEN, the current directory would be matlabroot\toolboxmatlab\iofun\ and a file without a path is searched there. If you call FOPEN(WHICH(FileName)), the file is searched in matlabroot\toolbox\matlab\general at first...
> > I think, the frequent changes when calling "SIN(a + ABS(b))" would slow down Matlab completely: The + should change the current folder to the location of "plus.m" --- except if [b] is an object with an own PLUS operator...
>
> Allow me to disagree: the pop-up dialog only shows up if you are trying to run an m-file which is located in a directory which is not included in the path. If you run FOPEN, for example, MATLAB knows where to search for it.
>
> When the dialog shows up and when you press "Change directory", I assume that MATLAB adds this directory temporarily to the path and removes it in the end. This is the behavior I would like to be possible to be set as default in some check box in the MATLAB Preferences.
>
> Andre

-------------------------------------------------------------
I gotta agree with Andre on this. I have dozens of MATLAB projects,
each in it's own folder, and I frequently switch between them by
clicking of the name in the bottom line of the code editor. If I
forgot to do the extra step of changing the current folder, it's
always a pain to either have a bunch of error messages (if it tries to
run it), or else be presented with the "Add to path" "Change folder"
prompt. Having it switch automatically would eliminate that pain for
me. At least give me the opportunity of having that be an option/
preference I can either select or not.

Subject: "Change directory" dialog box

From: Jan Simon

Date: 9 Oct, 2009 14:55:25

Message: 10 of 15

Dear Andre!

> > > is there any way (other than adding a line of code in every file) to set the MATLAB preferences such that the default behavior for running m-files is to change the current directory to the directory where the file is located ?

> Allow me to disagree: the pop-up dialog only shows up if you are trying to run an m-file which is located in a directory which is not included in the path. If you run FOPEN, for example, MATLAB knows where to search for it.

So I think, I did not understand, when you want to change the directory.

What do you mean by "running m-files"?

Do you mean the behaviour of the Run-Button from the editor? Then the change of the directory happens only once at pressing the button.
I meant the currently running M-file, which is a completely different story.

Kind regards, Jan

Subject: "Change directory" dialog box

From: André Brito

Date: 9 Oct, 2009 16:20:21

Message: 11 of 15

"Jan Simon" <matlab.THIS_YEAR@nMINUSsimon.de> wrote in message <hanist$p3$1@fred.mathworks.com>...
> So I think, I did not understand, when you want to change the directory.
>
> What do you mean by "running m-files"?
>
> Do you mean the behaviour of the Run-Button from the editor? Then the change of the directory happens only once at pressing the button.
> I meant the currently running M-file, which is a completely different story.
>
> Kind regards, Jan

Yes, I meant the the run-button in the editor! Sorry about the mix up.

Andre

Subject: "Change directory" dialog box

From: ImageAnalyst

Date: 9 Oct, 2009 16:45:48

Message: 12 of 15

On Oct 9, 12:20 pm, "Andre Brito" <andre_brit...@hotmail.com> wrote:
> "Jan Simon" <matlab.THIS_Y...@nMINUSsimon.de> wrote in message <hanist$p...@fred.mathworks.com>...
> > So I think, I did not understand, when you want to change the directory.
>
> > What do you mean by "running m-files"?
>
> > Do you mean the behaviour of the Run-Button from the editor? Then the change of the directory happens only once at pressing the button.
> > I meant the currently running M-file, which is a completely different story.
>
> > Kind regards, Jan
>
> Yes, I meant the the run-button in the editor! Sorry about the mix up.
>
> Andre

-------------------------------------
Well for me, I'd just as soon have it change directory immediately
upon selecting the m-file that you're going to work with. So you
click on the filename in the "task bar" (or whatever you call it) and
the code window switches to the code for that m-file, and then I'd
like it to change the current folder right then - why wait until you
run it (by clicking on the green triangle). If you changed only after
you ran it, then you have this intermediate state where the displayed
code and the current folder don't match. Then after you click "run"
they do match. So I don't want the discrepancy of having different
current folders when looking at some code, depending on whether I ran
the code or not.

Subject: "Change directory" dialog box

From: Jan Simon

Date: 9 Oct, 2009 19:19:03

Message: 13 of 15

Dear ImageAnalyst!

> Well for me, I'd just as soon have it change directory immediately
> upon selecting the m-file that you're going to work with. So you
> click on the filename in the "task bar" (or whatever you call it) and
> the code window switches to the code for that m-file, and then I'd
> like it to change the current folder right then - why wait until you
> run it (by clicking on the green triangle). If you changed only after
> you ran it, then you have this intermediate state where the displayed
> code and the current folder don't match. Then after you click "run"
> they do match. So I don't want the discrepancy of having different
> current folders when looking at some code, depending on whether I ran
> the code or not.

There are 2 fundamental concepts for managing paths: the current directory and the "path" mechanism, which means a list of paths in a specific order to look for searches files in.
They are really good concepts, therefore we find them for a variety of operating systems and development environments. Of course Matlab must be compatible with these established concepts.

1. If I'm working with an M-file, I absolutely do not want the current folder to be changed. E.g. if I CD to a directory with the data file "TestData.mat" and debug the processing function, it would be a hard nonsense, if the file cannot be found anymore due to a missing absolute path. This would destroy the current directory concept!

2. When I hit the RUN button, the function should behave exactly as if it is called by a standard function call. Otherwise such difference would make the RUN button useless for me. The message, that a function is not in the current path, is valuable for me! E.g. it helps to detect an incomplete path definition. Neither changing the current path nor appending the current path to the Matlab PATH automatically would satisfy my expectations!

3. Running a function (from command line, main program or RUN button), path management and the current directory concept are 3 distinct jobs. Mixing them would reduce the power of the these concepts.

I hope the discussion goes on. Perhaps I miss some points in your ideas.
Kind regards, Jan

Subject: "Change directory" dialog box

From: Nathan

Date: 9 Oct, 2009 19:29:31

Message: 14 of 15

On Oct 9, 12:19 pm, "Jan Simon" <matlab.THIS_Y...@nMINUSsimon.de>
wrote:
> Dear ImageAnalyst!
>
> > Well for me, I'd just as soon have it change directory immediately
> > upon selecting the m-file that you're going to work with.  So you
> > click on the filename in the "task bar" (or whatever you call it) and
> > the code window switches to the code for that m-file, and then I'd
> > like it to change the current folder right then - why wait until you
> > run it (by clicking on the green triangle).  If you changed only after
> > you ran it, then you have this intermediate state where the displayed
> > code and the current folder don't match.  Then after you click "run"
> > they do match.  So I don't want the discrepancy of having different
> > current folders when looking at some code, depending on whether I ran
> > the code or not.
>
> There are 2 fundamental concepts for managing paths: the current directory and the "path" mechanism, which means a list of paths in a specific order to look for searches files in.
> They are really good concepts, therefore we find them for a variety of operating systems and development environments. Of course Matlab must be compatible with these established concepts.
>
> 1. If I'm working with an M-file, I absolutely do not want the current folder to be changed. E.g. if I CD to a directory with the data file "TestData.mat" and debug the processing function, it would be a hard nonsense, if the file cannot be found anymore due to a missing absolute path. This would destroy the current directory concept!
>
> 2. When I hit the RUN button, the function should behave exactly as if it is called by a standard function call. Otherwise such difference would make the RUN button useless for me. The message, that a function is not in the current path, is valuable for me! E.g. it helps to detect an incomplete path definition. Neither changing the current path nor appending the current path to the Matlab PATH automatically would satisfy my expectations!
>
> 3. Running a function (from command line, main program or RUN button), path management and the current directory concept are 3 distinct jobs. Mixing them would reduce the power of the these concepts.
>
> I hope the discussion goes on. Perhaps I miss some points in your ideas.
> Kind regards, Jan

What I understood is that they would like to be able to disable that
dialog that pops up so they don't have to repeatedly push the "change
directory" button.
There should be an option that when you click a button (such as change
directory) in that dialog box, that asks you if you would like to keep
this setting for future reference rather than having to do it each and
every time.
For people like you, you could ignore that option and have the dialog
pop up every time.
For people like Image Analyst and Andre, they could select the option
to hide the dialog box and remember their choice the first time they
selected "change directory".
I would assume that you could re-enable the popup dialog through
matlab preferences (if this is ever implemented), so you can see the
annoying (yet sometimes helpful) dialog pop up once again.

I think each of you have valid points, but they can coexist in this
functionality.

-Nathan

Subject: "Change directory" dialog box

From: ImageAnalyst

Date: 9 Oct, 2009 23:52:07

Message: 15 of 15

Jan:
I know all that and understand all that, and your points are valid for
the way you like to operate. Nonetheless my opinion remains
unchanged. By the way, in all important m-files (those which I plan
on other users running on other computers), I ALWAYS constuct full
paths with fullfile() even if I have to go through difficulties to get
the path of the running mfile or executable. I just like to make
things explicit and remove all ambiguity and requirement or assumption
that the user has a certain search path set up. If you like to
manually set the current folder, that's fine - I respect your
opinion. For me, I'd just like the convenience of MATLAB
automatically doing what I always do anyway, and that is to push the
"Change Directory" button when it comes up.

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
feature request Andre Brito 8 Oct, 2009 06:54:35
default behavior Andre Brito 7 Oct, 2009 12:13:38
dialog box Andre Brito 7 Oct, 2009 12:13:27
change directory Andre Brito 7 Oct, 2009 12:13:16
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com