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

 Respuesta aceptada

Rik
Rik el 21 de Feb. de 2017
Editada: Rik el 22 de Feb. de 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 comentarios

Liam Thurston
Liam Thurston el 22 de Feb. de 2017
Thank you for your answer.
I have tried using the code you have suggested but for some reason it only creates a 1x1 cell which contains an empty array. I need this piece of code to store all the files in the correct order as in the order they are in within the folder. Can this be done?
Any help would be much appreciated.
Rik
Rik el 22 de Feb. de 2017
What have you actually put in? Because it should create a length(list)x1 cell. So if you get a 1x1, you have changed something to remove elements from container.
Liam Thurston
Liam Thurston el 22 de Feb. de 2017
I have sorted it the cell contains the correct amount of arrays. Is there a way of knowing that matlab has brought the files into the program in the correct order?
Rik
Rik el 22 de Feb. de 2017
You do not need to sort anything anymore. The order is dictated by the for-loop. What is the code that you used? Because if you didn't use exactly my code, I don't know what to debug.
Liam Thurston
Liam Thurston el 22 de Feb. de 2017
Editada: Liam Thurston el 22 de Feb. de 2017
I have sorted it. Is it possible to change the code if the file does not begin at Frame1.jpg, say it starts at Frame9.jpg? Thank you very much for your help.
@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 el 22 de Feb. de 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

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Data Type Conversion en Centro de ayuda y File Exchange.

Preguntada:

el 21 de Feb. de 2017

Editada:

Rik
el 22 de Feb. de 2017

Community Treasure Hunt

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

Start Hunting!

Translated by