Issue with length function

7 ビュー (過去 30 日間)
julian gaviria
julian gaviria 2022 年 3 月 14 日
コメント済み: Rik 2022 年 3 月 14 日
Why does the length function never stops the iteration?
Context: Using copyfile function (matlab2018b) for copying and pasting indexed files. To note, the files are rightly copied and pasted. But the iteration never ends. Even if Idelet the files in the destination folder, it keeps pasting them.
%%%
clc
clear
SourcePath ='\\nasac-m2.isis.unige.ch\m-GAubry\GAubry\YODA\RAW_DATA\Live\S008\V1\';
Participant = 'Session_2\scans\87627';
SourceFolder = fullfile([SourcePath,Participant],'RUN_1_MIST_1_0004');
DistPath ='\\nasac-m2.isis.unige.ch\m-GAubry\GAubry\YODA\jg\prp_glm\Sub_08\';
DistFolder = fullfile(DistPath,'FunRaw_S1');
if ~exist(DistFolder)
mkdir(DistFolder)
end
for i=1:length(dir(fullfile(SourceFolder,'f*')))
copyfile(fullfile(SourceFolder,'f*'),DistFolder);
end

採用された回答

Geoff Hayes
Geoff Hayes 2022 年 3 月 14 日
@julian gaviria - why aren't you using the i in your loop?
for i=1:length(dir(fullfile(SourceFolder,'f*')))
copyfile(fullfile(SourceFolder,'f*'),DistFolder);
end
So it would seem that the above code would try to copy the full set of files for every file in the directory that matches the condition. Perhaps the following might help
filesToCopy = dir(fullfile(SourceFolder,'f*'));
for i=1:length(filesToCopy)
copyfile(fullfile(SourceFolder,filesToCopy(i).name),DistFolder);
end
  2 件のコメント
julian gaviria
julian gaviria 2022 年 3 月 14 日
It worked, Thanks @Geoff Hayes & @VBBV
the Line suggested by @VBBV were simpler:
copyfile(fullfile(SourceFolder,'f*'),DistFolder);
Just for learining porpuses:
As @Geoff Hayes rigthtly mentioned, my code tried to copy the full set of files for every file in the directory that matches the condition:
for i=1:length(dir(fullfile(SourceFolder,'f*')))
copyfile(fullfile(SourceFolder,'f*'),DistFolder);
end
Why matlab never ended the iteration in this case? I see no misuse of the "length" function.
Thank you all again for your help.
Best,
Rik
Rik 2022 年 3 月 14 日
You may not see a misuse, but I'm of the opinion you should see a bad habit. Did you intend the number of elements? Then you can more reliably get that with numel. Did you really want max(size(X))? I have never seen anyone who actually wanted that.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by