Having Matlab automatically choose the directory I am in

9 vues (au cours des 30 derniers jours)
charles atlas
charles atlas le 11 Mar 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 commentaire
Adam
Adam le 11 Mar 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.

Connectez-vous pour commenter.

Réponse acceptée

the cyclist
the cyclist le 11 Mar 2015
Modifié(e) : the cyclist le 11 Mar 2015
pwd returns the current directory. On Unix-based machines, the tilde (~) is shorthand for the current directory.

Plus de réponses (1)

Image Analyst
Image Analyst le 11 Mar 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.

Catégories

En savoir plus sur Startup and Shutdown dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by