Having Matlab automatically choose the directory I am in

10 ビュー (過去 30 日間)
charles atlas
charles atlas 2015 年 3 月 11 日
回答済み: Image Analyst 2015 年 3 月 11 日
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 件のコメント
Adam
Adam 2015 年 3 月 11 日
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.

サインインしてコメントする。

採用された回答

the cyclist
the cyclist 2015 年 3 月 11 日
編集済み: the cyclist 2015 年 3 月 11 日
pwd returns the current directory. On Unix-based machines, the tilde (~) is shorthand for the current directory.

その他の回答 (1 件)

Image Analyst
Image Analyst 2015 年 3 月 11 日
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.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by