make cd() work more efficiently or a better function to do what I am looking for
10 ビュー (過去 30 日間)
古いコメントを表示
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
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 件)
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
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
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 Center および File Exchange で Search Path についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!