Having Matlab automatically choose the directory I am in
조회 수: 2 (최근 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
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
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.
댓글 수: 0
추가 답변 (1개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!