MATLAB: Copy a string from filename and detect a new file in the folder

MATLABreadfile loadfile

Hi,
I have a list of files like that :
data_acc_13042012.mat
data_acc_15042012.mat
data_acc_25042012.mat
data_acc_02052012.mat
the numbers correspond to date, for ex : 13/04/2012
Firstly,I would like to extract only the numbers in the filename and save them to a vector "date". Then I will display my data following this vector "date".
Secondly, for each time I run my code, I would like to detect if a new data file is added, for ex : data_acc_10052012.mat, etc. If yes, I redo the extraction of date and add it to the vector "date".
May anyone have an idea to do all these things please ?
Thanks for all your answers
Winn

Best Answer

function ShowFiles(Folder)
persistent shownAlready
list = dir(fullfile(Folder, '*.mat'));
name = {list.name};
if ~isempty(shownAlready)
name_bak = name;
name = setdiff(name, shownAlready);
shownAlready = name_bak;
end
blockStr = sprintf('%s;', name{:});
number = sscanf(blockStr, 'data_acc_%2d%2d%4d.mat;');
dateV = transpose(reshape(number, 3, []));
dateD = datenum(dateV(:, 3), dateV(:, 2), dateV(:, 1));
[dum, order] = sort(dateD);
for i = order
<do something with the file: fullfile(Folder, name{i})>
end