Using movefile function in existing and unexisting files
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
julian gaviria
el 7 de Dic. de 2022
Comentada: julian gaviria
el 8 de Dic. de 2022
The following use of the "movefile" function works only if the requested files exist. But it yields error messages and stops the iteration when the files do not exist:
DistPath_MT = SourceFolder;
DistFolder_MT = fullfile(DistPath_MT,'MT');
if ~exist(DistFolder_MT )
mkdir(DistFolder_MT )
end
DistPath_T1w = SourceFolder;
DistFolder_T1w = fullfile(DistPath_T1w,'T1w');
if ~exist(DistFolder_T1w)
mkdir(DistFolder_T1w)
end
DistPath_PD = SourceFolder;
DistFolder_PD = fullfile(DistPath_T1w,'PD');
if ~exist(DistFolder_PD)
mkdir(DistFolder_PD)
end
movefile(fullfile(SourceFolder,'*_MT*'),DistFolder_MT);
movefile(fullfile(SourceFolder,'*_T1w*'),DistFolder_T1w);
movefile(fullfile(SourceFolder,'*_PD*'),DistFolder_PD );
How can I identify the target files in the SourceFolder , and move them to DistFolder when some of them exist while others don't? I unsuccesfully tried out something like:
if ~exist(DistFolder_MT)
movefile(fullfile(SourceFolder,'*_MT*'),DistFolder_MT)
end
%...the same for '*_T1w*' and '*_PD*'
Many thanks in advance
1 comentario
Respuesta aceptada
Stephen23
el 7 de Dic. de 2022
Editada: Stephen23
el 7 de Dic. de 2022
Capture the error as an output, and the error will not be thrown when you call the function:
[X,Y,Z] = movefile('doesnotexist*.txt','subdir')
3 comentarios
Stephen23
el 8 de Dic. de 2022
Editada: Stephen23
el 8 de Dic. de 2022
" However, I still do not manage to integrate the mentioned line to avoid interruption in the iteration when the "movefile" function does not find one of the requested files."
Because your code still does not return any output arguments from MOVEFILE.
Here are the modified lines of code:
[X,Y,Z] = movefile(fullfile(SourceFolder,'*_MT*'),fullfile(SourceFolder,[newParticipant,'_MT']));
[X,Y,Z] = movefile(fullfile(SourceFolder,'*_T1w*'),fullfile(SourceFolder,[newParticipant,'_T1w']));
[X,Y,Z] = movefile(fullfile(SourceFolder,'*_PD*'),fullfile(SourceFolder,[newParticipant,'_PD']));
% ^^^^^^^^^^ this is exactly what I showed in my answer.
Más respuestas (0)
Ver también
Categorías
Más información sobre System Composer en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!