필터 지우기
필터 지우기

Can Matlab read the most recent made file in the default folder?

조회 수: 58 (최근 30일)
C Zeng
C Zeng 2015년 4월 14일
편집: Jan 2022년 11월 8일
Hello, just want to know if Matlab can import the file that is most recent made based on their modified date and time?
I have several Excel files and want it read and do analysis?
Thanks.

채택된 답변

Jan
Jan 2022년 2월 26일
편집: Jan 2022년 11월 8일
A summary of the comments:
d = dir('somefolder/*txt');
[~, index] = max([d.datenum]);
youngestFile = fullfile(d(index).folder, d(index).name); % [EDITED], typo fixed
% Thanks, Andres Morales

추가 답변 (2개)

pfb
pfb 2015년 4월 14일
Hi,
you could get the excel files with
d= dir('*xls');
and then compare the dates. These are in
d(j).date
You probably better convert them to numbers to compare them
dd = zeros(length(d));
for j = 1:length(d)
dd(j) =datenum(d(j).date);
end
[tmp i]=max(dd);
load(dd(i).name)
  댓글 수: 5
Yan Kai Lai
Yan Kai Lai 2022년 2월 26일
편집: Yan Kai Lai 2022년 2월 26일
I used the answer by pfb to read the most recent txt file. To make the answer more complete:
d = dir('somefolder/*txt');
dd = zeros(length(d), 1); % to init as vector instead of square matrix
for j = 1:length(d)
dd(j) = datenum(d(j).date);
end
[~, i] = max(dd); % tmp is the datenum, which is not necessary
lines = readlines(fullfile(d(i).folder, d(i).name)) % should be d instead of dd.
Stephen23
Stephen23 2022년 2월 26일
편집: Stephen23 2022년 2월 26일
Converting to DATENUM is not required because the DIR output structure already contains serial date numbers, so that superfluous loop can be simply replaced by this:
dd = [d.datenum];

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


Walter Roberson
Walter Roberson 2022년 2월 26일
편집: per isakson 2022년 8월 1일

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by