How to read a file in series of files saved in a order.
6 views (last 30 days)
Hello everyone, I have saved files as IR_A_01_01.png, IR_B_01_02.png, IR_C_01_03.png, IR_D_01_04.png, IR_A_01_05.png......the string A-B-C-D-A is a cycle. The third digit 01 doesnt change but is important. The last digit increaments everytime. Everytime I run the program I have to read one image and in next run next image. So how I can read the files one after the other?
What I have come up with is to have a global variable which will be initialised to 01(global num) and increaments at end.And the string will be initialised with letter='A'which will change to 'B' at end. So the third digit wont change. So if I say picture='IR_%s_01_%d.png','letter','num'; I should be getting the file but I am unable. I have changed the directory before I did this. So i just need to program so that i can read them one after the other.
Would appreciate any input. Thank you all.
Shivam on 30 Jul 2022
You can sort files by sorting modification date.
All you would need is:
D = '.'; % folder path
files = dir(fullfile(D,'*.dat'));
[~,idx] = sort([files.datenum]);
This idx contains the values from oldest files(idx(1)) to newest files(idx(end)). Use 'descend' as an option in sort() if you want to edit the newest first.
for k = 1:numel(idx)
F = fullfile(D,idx(k).name)
... import/process file F