Concatenate a file path in copyfile

136 ビュー (過去 30 日間)
Charles Cummings
Charles Cummings 2019 年 1 月 22 日
コメント済み: Charles Cummings 2019 年 1 月 22 日
I am attemtping to create a series of files I load into a script . This series of files will have a variable changed in each of them. What i'd like to do is create the variable, the directory of the scenario file, and, using copyfile, move the desired files from the template scenario to the new scenario file. The file name is currently concatenated correctly using mkdir, but cant seem to figure out how to concatenate the file path using the value of the variable I call 'noSpaceBetweenUs,' which I need to do both for the copyfile line, and the cd line in the code.
function startup
cd ~/Desktop/program/scenario/
%
for X= 100:100:500
noSpaceBetweenUs=mkdir(['ScenarioSpacing' num2str(X)]) %
copyfile(['scenario','/*.prm'],'~/Desktop/program/scenario/' convertStringsToChars(noSpaceBetweenUs)]) %(i want the value of noSpaceBetweenUs after that forward slash)
cd(['~/Desktop/program/Scenario' convertStringsToChars(noSpaceBetweenUs)])
%under 'scenario' should be a number of files of my choosing with parameter files
% each iteration through the loop should be creating a file and placing it in the desired location.
end
Appreciate the help!
  2 件のコメント
Stephen23
Stephen23 2019 年 1 月 22 日
Do NOT use cd in code. Changing directory is slow and makes debugging much harder.
All MATLAB functions that read/write data files accept absolute/relative filenames, so there is absolutely no need to change the current directory. Using absolute/relative filenames is faster than changing the current directory.
Do not use concatenations to build filepaths. It is recommended to use fullfile, which automatically handles the file separator character.
Charles Cummings
Charles Cummings 2019 年 1 月 22 日
Understood. Thanks for the explanation!

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

採用された回答

Jan
Jan 2019 年 1 月 22 日
Do not use cd to affect the current directory. Remember that a callback of a GUI or timer can change the current folder unexpectedly. Therefore relying on cd is fragile. It is much easier to use absolute path names.
function startup
base = '~/Desktop/program/scenario/';
for X= 100:100:500
folder = fullfile(base, ['ScenarioSpacing', num2str(X)]);
[status, msg, msgID] = mkdir(folder);
if status == 0 % Never create a file/folder without catching errors
error(msgID, msg);
end
copyfile(fullfile(base, 'scenario', '*.prm'), folder);
end
end
But what is the actual problem?
copyfile(['scenario','/*.prm'], ...
'~/Desktop/program/scenario/' convertStringsToChars(noSpaceBetweenUs)])
%(i want the value of noSpaceBetweenUs after that forward slash)
There is a missing [ . noSpaceBetweenUs is a char vector already, so there is no reason to convert it from type string to char. Is there another problem? If so, please mention it.
  2 件のコメント
Charles Cummings
Charles Cummings 2019 年 1 月 22 日
Actually that was a mistype, in my script it was typed 'correctly.'
What you showed is exactly what I wanted, so would it have worked the way I was trying or was it more just a sloppy way?
When I tried using copyfile in my script it produced an error along the lines of 'needing a character vector input.'
Charles Cummings
Charles Cummings 2019 年 1 月 22 日
Thanks for the help!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSearch Path についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by