Organising/Sorting imported files into matlab

1 view (last 30 days)
Hello,
I have a series of frames (files) that I would like to read into matlab with each file called Frame1.jpg, Frame2.jpg and so on. I was wondering if there is anyway to read these into matlab in order? Matlab organises them all to using the first integer to begin with, so it reads Frame1.jpg, Frame.10jpg and so on.
Any help would be much appreciated.
Kind regards
Liam

Accepted Answer

Rik
Rik on 21 Feb 2017
Edited: Rik on 22 Feb 2017
You can use the num2str function, but you should use sprintf instead (string-print-formatted, use fprintf to print formatted things to files):
list=1:10;%or list=[9 18 20]; or whatever list you need
container=cell(length(list),1);
for n=1:length(list)
filename = sprintf('Frame%d.jpg', list(n));
container{n}=imread(filename);
end
  7 Comments
Jan
Jan on 22 Feb 2017
@Rik: ?
filename = num2str(['Frame' num2str(list(n)) '.jpg'])
One num2str too much. I prefer:
filename = sprintf('Frame%d.jpg', list(n));
because it is shorter and has less overhead than num2str and a concatenation afterwards.
Rik
Rik on 22 Feb 2017
Ah. I must learn to re-read my answers if I write them at night. Thanks for the correction. And yes, sprintf is better, but old ways die hard... I'll edit my answer.
@Liam: yes, you can put anything you like in the vector list

Sign in to comment.

More Answers (0)

Categories

Find more on Shifting and Sorting Matrices 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!