add file to matlab using last date

I have a bunch of files in a folder, with names that look like this:
squad1_1910_St4_Se8
squad1_1910_St2_Se3
squad1_1910_St1_Se6
squad1_1910_St4_Se5
I have a code that automatically updates the file using regexprep, amongst other things. right now, the code chooses the file with user input, for instances:
root = 'E:\MATLAB';
user_input_squad = input('please enter squad number: ', 's');
user_input_experiment = input('please enter experiment name: ', 's');
user_input_stage = input('please enter stage: ', 's');
user_input_session = input('Please enter session: ', 's');
file = fullfile(root, sprintf('squad%s_1910_EXP%s_St1_Se%s', user_input_squad, user_input_experiment, user_input_session));
Is there a way for matlab to pull the last file added, without the need for user input? something that sorts the directory by date, and chooses the first one?
this would have to be for a bunch of files, as they change periodically. for instance, there would be 2 files named
squad1_1910_St4_Se8
the squad number is the only chnage to the name. so if a user inputs squad 2, is there a way to automatically pick only the latest squad 2 file, without touching the others?

 Respuesta aceptada

Image Analyst
Image Analyst el 20 de Dic. de 2019
You can use dir() to get the file date/time stamps.
folder = pwd; % Whatever folder you want.
filePattern = fullfile(folder, '**\*.txt')
dirListing = dir(filePattern)
fileDates = [dirListing.datenum]
% Sort in descending order (most recent, latest date as item #1)
[sortedDates, sortOrder] = sort(fileDates, 'Descend')
% Sort files in that order.
dirListing = dirListing(sortOrder); % Rearrange using the new sort order.
fileNames = {dirListing.name}
% Let user know the most recent file.
message = sprintf('The most recent file is %s.\nIt has a date time stamp of %s', ...
fileNames{1}, datestr(dirListing(1).datenum))
fprintf('%s\n', message);
uiwait(helpdlg(message));
Let me know if it works for you (after obvious modifications of course).

6 comentarios

avram alter
avram alter el 23 de Dic. de 2019
Editada: avram alter el 23 de Dic. de 2019
In your code, the line
filePattern = fullfile(folder, '**\*.txt')
should be edited by me to include the names, or that some syntax that lets matlab know its just some .txt file?
also, this folder 4 different ypes of experimental files in it, and each type will need to be automaticallly chosen at a specific time. each type is for a different squad number, but I only want the latest file for that squad to be brought up. there might be newer files for a different squad though.
Image Analyst
Image Analyst el 23 de Dic. de 2019
You can change the filePattern to be whatever you like.
You can parse the filenames however you want to include or exclude particular files for analysis.
avram alter
avram alter el 23 de Dic. de 2019
Editada: avram alter el 23 de Dic. de 2019
ah, So if I wanted it to search for the latest file with the word 'squad1' appearing in the name of the file, how would I do that?
Image Analyst
Image Analyst el 23 de Dic. de 2019
Editada: Image Analyst el 23 de Dic. de 2019
Let's say you're in a loop where you want to find only the ones you want - that contain some string in the filename. Then do
numFiles = length(fileNames);
filesIWant = false(numFiles, 1);
for k = 1 : numFiles
if contains(fileNames{k}, 'squad1', 'IgnoreCase', true)
% Add it to our list.
filesIWant(k) = true; % Mark this one for inclusion.
else
% File name does not contain 'squad1' so nothing to do.
continue; % Skip to bottom of loop
end
end
% Extract only the ones we want
fileNames = fileNames(filesIWant);
avram alter
avram alter el 23 de Dic. de 2019
thanks for all the help so far! how would I insert the newest bit of code into the one that looks of it using the latest date? I don't know matlab so well, so I apologize if that is an obvious question.
If you already have identified the most recent file's name, then you can simply see if that filename contains 'squad1' by using contains():
if contains(mostRecentFileName, 'squad1', 'IgnoreCase', true)
% Then the most recent file contains the string 'squad1' in its name.
end

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Language Fundamentals en Centro de ayuda y File Exchange.

Productos

Versión

R2019b

Etiquetas

Preguntada:

el 20 de Dic. de 2019

Comentada:

el 24 de Dic. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by