Borrar filtros
Borrar filtros

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

58 visualizaciones (últimos 30 días)
C Zeng
C Zeng el 14 de Abr. de 2015
Editada: Jan el 8 de Nov. de 2022
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.

Respuesta aceptada

Jan
Jan el 26 de Feb. de 2022
Editada: Jan el 8 de Nov. de 2022
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

Más respuestas (2)

pfb
pfb el 14 de Abr. de 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 comentarios
Yan Kai Lai
Yan Kai Lai el 26 de Feb. de 2022
Editada: Yan Kai Lai el 26 de Feb. de 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.
Stephen23
Stephen23 el 26 de Feb. de 2022
Editada: Stephen23 el 26 de Feb. de 2022
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];

Iniciar sesión para comentar.


Walter Roberson
Walter Roberson el 26 de Feb. de 2022
Editada: per isakson el 1 de Ag. de 2022

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by