How to put a struct files into a new folder

조회 수: 4 (최근 30일)
Coco
Coco 2019년 7월 19일
편집: Nagamani Yaadavalli 2020년 12월 8일
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
Walter Roberson
Walter Roberson 2019년 7월 19일
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 2019년 7월 22일
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'.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2019년 7월 22일
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
Coco
Coco 2019년 7월 23일
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.
Nagamani Yaadavalli
Nagamani Yaadavalli 2020년 12월 8일
편집: Nagamani Yaadavalli 2020년 12월 8일
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

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Coco
Coco 2019년 7월 22일
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)

카테고리

Help CenterFile Exchange에서 Environment and Settings에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by