필터 지우기
필터 지우기

Read files in a folder with dates on them

조회 수: 13 (최근 30일)
lena kappa
lena kappa 2023년 6월 14일
댓글: lena kappa 2023년 6월 14일
Hi everyone, I have a folder with multiple.mat files that are named : Profile_YYYY_Mon for example Profile_1990_Jan.mat the monts are Jan Feb Mar until Dec .How can i read each file in the correct chronological order and then use it to do something inside a for loop?

채택된 답변

Stephen23
Stephen23 2023년 6월 14일
편집: Stephen23 2023년 6월 14일
The actual solution is to parse the filenames that you gave. Lets create some fake files with fake data:
X=1;save Profile_1990_Jan.mat X
X=2;save Profile_1990_Feb.mat X
X=3;save Profile_1990_Mar.mat X
X=4;save Profile_1990_Apr.mat X
Now lets read them in chronological order:
P = '.'; % absolute/relative path to folder where files are saved.
S = dir(fullfile(P,'Profile*.mat')); % alphabetical order.
I = regexp({S.name},'\d+_\w+','match','once');
DT = datetime(I,'InputFormat','y_MMM')
DT = 1×4 datetime array
01-Apr-1990 01-Feb-1990 01-Jan-1990 01-Mar-1990
[~,X] = sort(DT);
S = S(X); % sort into chronological order
for k = 1:numel(S)
F = fullfile(P,S(k).name);
D = load(F);
D.X % do whatever you want with the imported data
end
ans = 1
ans = 2
ans = 3
ans = 4
Another approach without REGEXP:
S = dir(fullfile(P,'Profile*.mat')); % alphabetical order
DT = datetime({S.name},"InputFormat","'Profile'_y_MMM'.mat'")
DT = 1×4 datetime array
01-Apr-1990 01-Feb-1990 01-Jan-1990 01-Mar-1990
[~,X] = sort(DT);
S = S(X); % sort into chronological order.
for k = 1:numel(S)
F = fullfile(P,S(k).name);
D = load(F);
D.X % do whatever you want with the imported data
end
ans = 1
ans = 2
ans = 3
ans = 4
  댓글 수: 1
lena kappa
lena kappa 2023년 6월 14일
Thank you so much @Stephen23! Both answers work but is one better that the other?

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

추가 답변 (2개)

Image Analyst
Image Analyst 2023년 6월 14일
편집: Image Analyst 2023년 6월 14일
How about something like this to sort by file time/date stamp:
folder = pwd; % Wherever you want.
filePattern = fullfile(folder, 'Profile*.mat');
fileList = dir(filePattern);
[~, sortOrder] = sort([fileList.datenum], 'ascend');
fileList = fileList(sortOrder)
allFileNames = fullfile(folder, {fileList.name});
for k = 1 : numel(allFileNames)
thisFileName = allFileNames{k};
fprintf('Now processing "%s" with a date of %s.\n', thisFileName, fileList(k).date);
% Now do something with the file.
end
  댓글 수: 1
lena kappa
lena kappa 2023년 6월 14일
@Image Analyst Thank you for your time, unfortunately this wasn't what I needed.

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


Image Analyst
Image Analyst 2023년 6월 14일
편집: Image Analyst 2023년 6월 14일
How about something like this to sort by the 3 letter month string:
months = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'}
folder = pwd; % Wherever you want.
filePattern = fullfile(folder, 'Profile*.mat');
fileList = dir(filePattern);
[~, sortOrder] = sort([fileList.datenum], 'ascend');
fileList = fileList(sortOrder)
allFileNames = fullfile(folder, {fileList.name})
for k = 1 : numel(months)
indexes = find(contains(allFileNames, months(k)));
for m = 1 : numel(indexes)
thisFileName = allFileNames{indexes(m)};
% See if this filename contains the k'th month.
% If it does, process it. If not, skip it.
fprintf('Now processing "%s" with a date of %s.\n', thisFileName, fileList(indexes(m)).date);
end
end
  댓글 수: 1
lena kappa
lena kappa 2023년 6월 14일
@Image Analyst Thank you for your time, unfortunately this wasn't what I needed.

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

카테고리

Help CenterFile Exchange에서 Embedded Coder Supported Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by