make cd() work more efficiently or a better function to do what I am looking for

조회 수: 9 (최근 30일)
jfrazie9
jfrazie9 2018년 11월 1일
댓글: Guillaume 2018년 11월 2일
I am trying to set up a directory change at the beginning of this code such that it will simply go into the stated folder and run so I do not have to make many copies of the same code. So far I have tried the following code
date=input('Enter date of Inversion (date on folder) ==>','s');
site=input('Enter site of data analysis ==>','s');
layering=input('Enter layering ratio used with the number of layers (as named) ==>','s');
cd ('F:\SCG_JW_3\JacksonWilsonWest3PostInversion\',date,'\',site,'_',layering,'_reports');
This works if I just put the directory in straight but am trying to make it more interactive if I can. When I run this I am getting an "Error using cd; Too many arguments."
Is there a way to make this work like this or is a different function better? I have tried a uigetdir() but the file types are not being supported.
  댓글 수: 1
Stephen23
Stephen23 2018년 11월 2일
편집: Stephen23 2018년 11월 2일
"I have tried a uigetdir() but the file types are not being supported"
uigetdir does not "support" anything to do with files, or "file types", or indeed files at all.
And even uigetfile and uiputfile do not care about "file types", except indirectly through the filename filter (which is merely an artifact of the filename, and does not actually check the file format at all).
But as you did not show us what you tried, we have no idea what you did wrong in your code.
In any case, cd is slow and makes debugging hard. Just use fullfile.

댓글을 달려면 로그인하십시오.

답변 (2개)

Walter Roberson
Walter Roberson 2018년 11월 1일
Use fullfile to construct the directory name.

Guillaume
Guillaume 2018년 11월 1일
The most efficient way to make cd() more efficient is to not use cd() at all. There is never any need to use cd() in matlab code and it's actually a dangerous thing to do (if you cd() into a directory that has a sum.m file, you'll suddenly be using that function instead of matlab own sum and when you cd out of it again, you'll revert back to the normal sum).
All of matlab functions that deal with files (such as load, xlsread, fopen, etc.) can read files whereever they are without needing to change the current directory. So, instead of
cd(somewhere);
data = load(somefile);
you use
data = load(fullfile(somewhere, somefile));
This will be faster as well.
To build the path out of several bits, as Walter said, use fullfile. You don't even need to write the directory separator ('\' or '/'), fullfile will use the correct one for your OS.
As for building the folder name, you can use concatenation [], or sprintf, so:
folder = [site, '_', layering, '_reports'];
fullpath = fullfile('F:\SCG_JW_3\JacksonWilsonWest3PostInversion', date, folder);
%whatever IO you intended after that, e.g.
data = load(fullpath, 'somefile')
or using sprintf which I find cleaner:
folder = sprintf('site_%s_reports', layering);
fullpath = fullfile('F:\SCG_JW_3\JacksonWilsonWest3PostInversion', date, folder);
%whatever IO you intended after that, e.g.
data = load(fullpath, 'somefile')
  댓글 수: 2
Walter Roberson
Walter Roberson 2018년 11월 2일
편집: Walter Roberson 2018년 11월 2일
"There is never any need to use cd() in matlab code"
run() appears to need to cd to the directory the script is in, in order to be sure that it gets that particular file as the one executed when it eval's. It cannot just use addpath() because the current directory is always first on the path, and the current directory must might happen to have a .m file with the same name.
Guillaume
Guillaume 2018년 11월 2일
Ok, then let's change it to There is never any need to use cd() in matlab code in a well designed program.
I would seriously question the robustness of any program that need to change directory in order to find the next piece of code to execute.
In any case, the common use case of cd is to access the data that is to be processed. There is never any reason to cd for that.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Search Path에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by