Selecting Files Within Subfolders

Hi, I have written the code:
>> path = {'/Users/Naomi/Desktop/GroupAnalysis/sub1/STRUC'};
>> movefile('s0-0007-00001-000001-01.nii', 'T1.nii')
Error using movefile
mv: rename /Users/naomi/Desktop/GroupAnalysis/s0-0007-00001-000001-01.nii to
/Users/naomi/Desktop/GroupAnalysis/T1.nii: No such file or directory
And I am receiving the above error message. Why is my file not being selected in the STRUC subfolder even if I am indicating it in the path?

Respuestas (2)

Image Analyst
Image Analyst el 18 de Oct. de 2018

0 votos

Do not overwrite path, the built-in variable or you will have problems.
Make sure the file exists. It's telling you your source file is not there.

5 comentarios

Naomi Gaggi
Naomi Gaggi el 18 de Oct. de 2018
Yes, the file is in the folder sub1/STRUC/ but the error is showing: /Users/naomi/Desktop/GroupAnalysis/T1.nii: No such file or directory So, I would like the program to select the file in the directory I specified: '/Users/Naomi/Desktop/GroupAnalysis/sub1/STRUC'
The change the current folder and call movefile(). If you want to do it in code you can use cd, NOT path:
cd('/Users/Naomi/Desktop/GroupAnalysis/sub1/STRUC');
movefile('s0-0007-00001-000001-01.nii', 'T1.nii')
Naomi Gaggi
Naomi Gaggi el 18 de Oct. de 2018
Thanks! Is there a way use cd in a loop? For example, I want to select path(1) = '/Users/Naomi/Desktop/GroupAnalysis/sub1/STRUC' then path (2) = '/Users/Naomi/Desktop/GroupAnalysis/sub2/STRUC'and in each path select file 's0-0007-00001-000001-01.nii' and rename it?
Image Analyst
Image Analyst el 18 de Oct. de 2018
Yes you can. But it's best not to use cd and to just use the full file name when you call movefile(). See the FAQ: https://matlab.wikia.com/wiki/FAQ#Where_did_my_file_go.3F_The_risks_of_using_the_cd_function.
Also, use exist(sourceFileName, 'file') before you call movefile() to make sure it exists first.
And, AGAIN DO NOT USE path AS THE NAME OF YOUR VARIABLE or you will regret it. I can't emphasize that enough.
Naomi Gaggi
Naomi Gaggi el 18 de Oct. de 2018
Thanks a lot!!

Iniciar sesión para comentar.

Jan
Jan el 18 de Oct. de 2018
Instead of using cd to modify the current folder, it is safer to use absolute paths:
for k = 1:10
Folder = sprintf('/Users/Naomi/Desktop/GroupAnalysis/sub%d/STRUC', k);
movefile(fullfile(Folder, 's0-0007-00001-000001-01.nii'), ...
fullfile(Folder, 'T1.nii'));
end

1 comentario

Naomi Gaggi
Naomi Gaggi el 18 de Oct. de 2018
Thank you! The program created a folder T1.nii within the folder STRUC, is there a way to change the file name rather than create a folder name?

Iniciar sesión para comentar.

Categorías

Más información sobre Search Path en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 18 de Oct. de 2018

Comentada:

el 18 de Oct. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by