Having Matlab automatically choose the directory I am in

2 Ansichten (letzte 30 Tage)
charles atlas
charles atlas am 11 Mär. 2015
Beantwortet: Image Analyst am 11 Mär. 2015
All,
I have a code that references the user directory that I am in:
userDir = uigetdir('C:\','Browse to station directory');
filePath = [userDir,'\station_data_',datestr(now,30),'.txt'];
fid = fopen(filePath,'w');
Then the rest of my code (which is not listed here) executes based on the directory that I choose. I will say that I always want to choose the directory that I am in (The one that is listed on the left side of Matlab under "current folder").
Is there any way to not have to prompt the user to input the directory I am in, and just have Matlab automatically use the directory that I am in when I run the code?
I want to eliminate the need to prompt the user to use the current folder that I am in by having him/her typing it in with the prompt:
userDir = uigetdir('C:\','Browse to station directory');
Any help is greatly appreciated. Respectfully, Charles
  1 Kommentar
Adam
Adam am 11 Mär. 2015
p = mfilename('fullpath')
can be very useful if you want a directory that will not change that you can use to create relative paths from. It returns the full path of the currently executing m-file.
If you are happy that your current user directory will always be the one you wish to append to though then the cyclist's answer below is the correct one.
I added this as a comment though as it is a useful function to know about when you need to use code relative to certain directories.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

the cyclist
the cyclist am 11 Mär. 2015
Bearbeitet: the cyclist am 11 Mär. 2015
pwd returns the current directory. On Unix-based machines, the tilde (~) is shorthand for the current directory.

Weitere Antworten (1)

Image Analyst
Image Analyst am 11 Mär. 2015
I would not do that. I virtually never use cd. I would take the advice of the FAQ and just keep track of and use the folder and use full filenames all the time. See the FAQ for code samples: http://matlab.wikia.com/wiki/FAQ#Where_did_my_file_go.3F_The_risks_of_using_the_cd_function.

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by