variable format issue using copyfile

14 ビュー (過去 30 日間)
dweather
dweather 2019 年 10 月 9 日
編集済み: Stephen23 2021 年 10 月 19 日
Hello Matlab community!
I am having what is probably a really simple issue using copyfile that I can't seem to debugg so hoping someone can help.
I am simply trying to copy an image file from one folder to another using specific paths. The code is here:
finalImgStr = (['RUN0',sprintf('%d',i), '_plot_0020.jpg']);
outputDir = ([projfilepath, '\Output\Run', sprintf('%d',i)]);
finalImg = fullfile(outputDir, finalImgStr);
finalImg1 = convertCharsToStrings(finalImg);
destDir1 = convertCharsToStrings(destDir);
copyfile ('finalImg1', 'destDir1');
When I try and run it I get an error saying 'no matching files were found'. However I have checked all the variables and the file paths are correct, and the image is in the source directory etc.
The variables were initially character arrays hence I have converted them to string array's (although the error is the same even if I leave them as characters...)
I'm assuming it's something to do with the formatting/data types but I can't work it out so any help would be appreciated!

採用された回答

Stephen23
Stephen23 2019 年 10 月 9 日
編集済み: Stephen23 2019 年 10 月 9 日
Your main bug is on this line:
copyfile('finalImg1', 'destDir1');
Your code looks for files literally called 'finalImg1', which is unlikely to be what you want.
You need to refer to the variables, and not use literal strings/character vectors:
copyfile(finalImg1, destDir1);
Some other improvements:
  1. use fullfile rather than concatenating paths.
  2. use sprintf with the complete name, rather than just the numeric part.
  3. pointless data-type conversion is... errr, pointless. Don't.
For example, something like this:
fnm = sprintf('RUN0%d_plot_0020.jpg',i);
sub = sprintf('Run%d',i);
old = fullfile(projfilepath,'Output',sub,fnm);
copyfile(old,destDir1);
Note that your leading zero is rather suspicous, I suspect that you actually want a leading zero but only for one digit (and not for two or more) in which case sprintf can handle this:
fnm = sprintf('RUN%02d_plot_0020.jpg',i);
  3 件のコメント
Michelle De Luna
Michelle De Luna 2021 年 10 月 18 日
@Stephen: Hi Stephen! I had a quick question that appears to be similar to the one you helped resolve here, so I thought I might try my luck to see if you see this message. I have an 800x1 char array ("relevant_files") with the name of .nc4 files that I would like to select from an existing folder and move to a new folder ("myFolder"). However, when I try to use the "relevant_files" variable with the copyfile function, I get an error message. I assume the issue is that I cannot read a variable using the function (I must explicitly write the name of the individual files I am looking for), but I was wondering if you had any tips I might be able to use. Any help would be greatly appreciated. Here's what I have so far:
relevant_files = [];
for i = 1:length(x)
year = num2str(x(i,1));
month = num2str(x(i,2), '%02.f');
day = num2str(x(i,3), '%02.f');
file_name = strcat('north_winds_Np.',year,month,day,'.nc4.nc4');
relevant_files = [relevant_files; file_name];
end
relevant_files
mkdir myFolder
movefile relevant_files myFolder
Stephen23
Stephen23 2021 年 10 月 19 日
編集済み: Stephen23 2021 年 10 月 19 日
The first bug is on these lines:
mkdir myFolder
movefile relevant_files myFolder
where you use command syntax instead of the required function syntax:
The second bug is that because you are trying to move multiple files using by their individual names (i.e. as opposed to use a wildcard character) you will have to loop over the names and call MOVEFILE for each one.
Note that those date strings are probably easier to generate using COMPOSE:
x = [1990,12,31;1984,2,29;1900,1,1]
x = 3×3
1990 12 31 1984 2 29 1900 1 1
compose('%04d%02d%02d',x)
ans = 3×1 cell array
{'19901231'} {'19840229'} {'19000101'}
Although, given that you will need a loop anyway (for MOVEFILE), you can simply call SPRINTF for each name inside the loop.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMultidimensional Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by