How do I read the last document from a directory

I´ll explain my question:
I have a directory in which new results are posted as new documents everyday and Matlab does a process with the newest to give a results file. How can I set an .m file to choose the newest file everytime?

 Accepted Answer

d=dir('*.mat'); % if you need mat files
dates={d.date};
files={d.files};
[~,idx]=sort(datenum(dates));
new_file=files{idx(end)}

Categories

Find more on Environment and Settings 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!