フィルターのクリア

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 件のコメント
Andres Morales
Andres Morales 2022 年 11 月 8 日
Seems like you have an extra ")" on the 3rd line.
Jan
Jan 2022 年 11 月 8 日
@Andres Morales: Thanks. I've fixed it.

サインインしてコメントする。

その他の回答 (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 Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by