Can Matlab read the most recent made file in the default folder?
Show older comments
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.
Accepted Answer
More Answers (2)
pfb
on 14 Apr 2015
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 Comments
C Zeng
on 14 Apr 2015
pfb
on 14 Apr 2015
datenum should take care of that.
If a file is more recent than another one, its datenum would be a larger number. Take a look at the documentation of datenum.
C Zeng
on 14 Apr 2015
Yan Kai Lai
on 26 Feb 2022
Edited: Yan Kai Lai
on 26 Feb 2022
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.
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
on 26 Feb 2022
Edited: per isakson
on 1 Aug 2022
0 votes
Categories
Find more on Calendar in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!