Having Matlab automatically choose the directory I am in

5 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by