How to put a struct files into a new folder

Hi guys,
After i use the following code to find files i want, how to put those files into the new folder? Thank you.
This code will give me the info of files in a struct such as name, path, size etc.
files = dir('D:\Data\**\*.mat')

4 commentaires

Walter Roberson
Walter Roberson le 19 Juil 2019
You are dealing with multiple folders. Should all of the files, no matter which original folder, be put into the same destination folder? Should there be a different sub-folder for each original folder? If so then how many levels of folder name should be retained in constructing the destination file name?
Coco
Coco le 19 Juil 2019
Hi Walter
Yes, multiples folder. All of the files should be put into the same destination folder. There are 3 levels of folders:
D:\Data\MDA\MDA cell1
level 1. Data
level 2. MDA
level 3. MDA cell1 ('cell1_Area.csv', ...)
MDA cell2 ('cell2_Area.csv', ...)
...
MDA cell10 ('cell10_Area.csv', ...)
Each lev3 subfolers has 100+ csv files, i only need one of those files (name is 'cell1_Area.csv', 'cell2_Area.csv', ..., 'cell10_Area.csv', ) be put into the destination folder. So the destination folder will have 10 csv files (let's say the name is 'Area'). The destination folder 'Area' should be the lev4 folder under lev3 folder 'MDA Matlab' as 'D:\Data\MDA\MDA Matlab\Area'.
if i dont express myself clearly, please let me.
Thank you very much for your help.
Walter Roberson
Walter Roberson le 19 Juil 2019
To confirm: even though each of the level 3 folders has 100+ files, you want to extract only the one where the "cell" number prefix matches the folder name? Does it happen to be the case that those are the only cell*_Area.csv files in those folders? For example, there is no cell1_Area.csv inside MDA cell2 that needs to be ignored because the cell# prefix does not match the folder name ?
Coco
Coco le 22 Juil 2019
Hi Walter
Sorry for the late reply.
Each csv file's name is unique and the cell# prefix matches the folder name.
For example: in folder 'MDA cell1', all of csv file is 'cell1_Area','cell1_distance'.... There is no cell2_* in folder 'MDA cell1'.

Connectez-vous pour commenter.

 Réponse acceptée

destdir = 'D:\Data\MDA\MDA Matlab\Area';
files = dir('D:\Data\MDA\MDA*\cell*_Area.csv');
fullnames = fullfile({files.folder}, {files.name});
if ~exist(destdir, 'dir')
mkdir(destdir);
end
copyfile(fullnames, destdir); %or movefile() if appropriate

4 commentaires

Coco
Coco le 22 Juil 2019
Modifié(e) : Coco le 22 Juil 2019
Hi Walter
Thank you. i tried it and i got the folder 'Area', but its empty. At the same time, i got an error: 'Error using copyfile
Arguments must contain a character vector.'
When i check 'fullnames', its a cell array. I cant figure out which went wrong. I need your help. Thank you. :)
Here is my original full name of files in matlab. Everything looks fine, but i can not run 'copyfile' successfully.
The following is my original code:
destdir = 'D:\Data\MDA231 Matlab\Area';
files = dir('D:\Data\MDA231\MDA231 Mito cell*_Statistics\MDA231 Mito cell*_Area.csv')
fullnames = fullfile({files.folder}, {files.name});
if ~exist(destdir, 'dir')
mkdir(destdir);
end
copyfile(fullnames, destdir);
files =
10×1 struct array with fields:
name
folder
date
bytes
isdir
datenum
Error using copyfile
Arguments must contain a character vector.
Please check fullnames:
filenames=char(fullnames)
filenames =
10×72 char array
'D:\Data\MDA231\MDA231 Mito cell10_Statistics\MDA231 Mito cell10_Area.csv'
'D:\Data\MDA231\MDA231 Mito cell1_Statistics\MDA231 Mito cell1_Area.csv '
'D:\Data\MDA231\MDA231 Mito cell2_Statistics\MDA231 Mito cell2_Area.csv '
'D:\Data\MDA231\MDA231 Mito cell3_Statistics\MDA231 Mito cell3_Area.csv '
'D:\Data\MDA231\MDA231 Mito cell4_Statistics\MDA231 Mito cell4_Area.csv '
'D:\Data\MDA231\MDA231 Mito cell5_Statistics\MDA231 Mito cell5_Area.csv '
'D:\Data\MDA231\MDA231 Mito cell6_Statistics\MDA231 Mito cell6_Area.csv '
'D:\Data\MDA231\MDA231 Mito cell7_Statistics\MDA231 Mito cell7_Area.csv '
'D:\Data\MDA231\MDA231 Mito cell8_Statistics\MDA231 Mito cell8_Area.csv '
'D:\Data\MDA231\MDA231 Mito cell9_Statistics\MDA231 Mito cell9_Area.csv '
cellfun(@(F) copyfile(F, destdir), filenames); %or movefile() if appropriate
Coco
Coco le 23 Juil 2019
Hi Walter
Thank you so much. It works very well. it saved me tons of time. I have 2000 csv files need to be sorted like this.
What if the output data has to save in an excel/csv format in a different folder. Here the the output of Analysis1 wants to save in a new excel/csv file in different folder or soeother directory. Please help me with this code.
% getting the folder directory of files you want to work
myFolder = '/Users/xxxxx/Desktop/xxxxx/Tests/27.11.2020 csv/Total';
% code for determining the file pattern that you have, * represents all the
% files
filePattern = fullfile(myFolder, '*.csv');
% code for addressing all the files
theFiles = dir(filePattern)
for k = 1: length(theFiles)
baseFileName = theFiles(k).name;
fullfilename = fullfile(theFiles(k).folder,baseFileName)
fprintf(1,fullfilename);
Test= dlmread(fullfilename,',',1,2)
theMeans = mean(Test,1)
Analysis1(:,k) = theMeans'
end

Connectez-vous pour commenter.

Plus de réponses (1)

Coco
Coco le 22 Juil 2019

0 votes

i also tried to save the file one by one, then it worked. just i dont know how to save them all automatically. any help? thank you.
f=fullfile('D:\','Data','MDA231','MDA231 Mito cell1_Statistics','MDA231 Mito cell1_Area.csv')
f =
'D:\Data\MDA231\MDA231 Mito cell9_Statistics\MDA231 Mito cell9_Area.csv'
>> copyfile(f, destdir)

Catégories

En savoir plus sur File Operations dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by