Concatenate a file path in copyfile

44 views (last 30 days)
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 Comments
Stephen23
Stephen23 on 22 Jan 2019
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 on 22 Jan 2019
Understood. Thanks for the explanation!

Sign in to comment.

Accepted Answer

Jan
Jan on 22 Jan 2019
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 Comments
Charles Cummings
Charles Cummings on 22 Jan 2019
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.'

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!