Moving files in matlab
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hey,
I'm new to matlab. I've made the basic tutorial and tried searching at google, but couldn't find any answers to my question.
I have a folder named DTI_scans. In this folder i have a lot of other folders (somehwere near 100) like 1002, 1003, 1004, etc. And in those folders i have one folder called DATA and alot of other files. In this data folder ('//DTI_scans/1005/DATA') i have again some files, but what I need to do in this DATA folder is to copy a file named 'data.nii' to a folder called DTI_network. I have to do this for every folder in DTI_scans (those 'numbered' folders). I'm having a bit trouble with this.
In the DTI_network folder, I also have to copy those data.nii files into the same 'numbered folders' as they originate from. So the data.ii file from 'DTI_scans\1005\Data' has to be copied to 'DTI_network\1005'.
I hope i've explained everything clearly. Could someone help me with this? Or put me in the right direction. I can't do this with movefile function right? Thanks for reading.
2 commentaires
Rik
le 17 Oct 2017
Why do you think you can't do that with movefile? You just need to do it in a loop. (Hint: find all the folder names with dir and enough asterisks)
Réponses (1)
Jan
le 17 Oct 2017
"//DTI_scans/1005/DATA" is a strange UNC path. Usually you have "//Host/Folder/" fixed, because this is the published path. The flexible part "1005" should appear at least one level deeper. I'm not sure if this causes troubles.
Which Matlab version are you using? Modern versions can obtain a list of files in subfolders also:
FileList = dir('//DTI_scans/**/data.nii');
for iFile = 1:numel(FileList)
inFolder = FileList(iFile).folder;
inFolder = strrep(inFolder, '/', '\'); % Avoid confusions
parts = strsplit(inFolder, '\');
outFolder = fullfile('DTI_Network', parts(end-1));
movefile(fullfile(inFolder, 'data.nii'), fullfile(outFolder, 'data.nii'));
end
Something like this will solve the problem: Find the files at first, then process them one by one in a loop. Split the folder name to extract the needed parts and use them to create the output folder. Then movefile can work as wanted.
5 commentaires
Rik
le 17 Oct 2017
Almost every entry in the FEX is a function (this one is no exception), which means you can simply download it and use it.
As for copying instead of moving: the function copyfile should do the trick there.
Jan
le 18 Oct 2017
Modifié(e) : Jan
le 18 Oct 2017
@Ibrahim: Of course I could write a small tool for a recursive search of files. This needs a few lines only. But the authors of the FEX submissions have done this already. So simply copy the source codes provided in the FileExchange. You can "install" this at your work exactly as if you would copy&paste code posted in the forum.
You could write a recursive search by your own also. Just try it, it is not hard. Well, I can't hold it out and give you an example:
function Files = FindFiles(Folder, Pattern)
% Get matching files in current folder:
List = dir(fullfile(Folder, Pattern));
Files = cellfun(@(s) fullfile(Folder, s), ...
{List(~[List.isdir]).name}, 'UniformOutput', 0);
% Get folders in current folder:
List = dir(Folder);
Folders = {List([List.isdir]).name};
Folders(strcmp(Folders, '.') | strcmp(Folders, '..')) = [];
for k = 1:numel(Folders) % Loop over all folders:
% Call this function recursively for subfolders:
Files = cat(2, Files, ...
FindFiles(fullfile(Folder, Folders{k}), Pattern));
end
Now I've added my own 2 cent to the other 137 solutions published on the MathWorks pages. :-)
Then:
FileList = FindFiles('//DTI_scans/', 'data.nii');
FileList = strrep(FileList, '/', '\'); ??? Maybe
for k = 1:numel(FileList)
inFolder = fileparts(FileList{k});
parts = strsplit(inFolder, '\');
outFolder = fullfile('DTI_Network', parts(end-1)); % ***
copyfile(FileList{k}, fullfile(outFolder, 'data.nii'));
end
Please replace the output folder at * correctly by a full path.
Voir également
Catégories
En savoir plus sur File Operations dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!