How can i change the "name" of my picture when using the imread('name') function?

5 views (last 30 days)
What i want to do is have a loop go through and read the name of every picture i have.
What i can't do is change the 'Name'(of the image) in a function like imread('Name') in the following way. (i believe this is because 'Name' is interpreted as a string)
(also the names of my pictures are integer values already)
for i = 1:n
image = imread('NameOfMyImage');
end % code

Accepted Answer

Christiaan
Christiaan on 2 Apr 2015
Dear Ely,
To import an image in MATLAB, you have to specify the extension. (i.e. Name.jpg)
In the code below you find a method how you can dynamically load multiple images in MATLAB with a for loop and give them the correct name. In the example is assumed you have JPG images. However you can extend the example yourself to also load PNG images. (for this example you need three images: Name_1.jpg, Name_2.jpg and Name_3.jpg.
clc;clear;close all;
% this is how you create a string in a for loop
for i = 1:3
name_of_the_file = sprintf('Name_%d.jpg', i)
name_of_the_image = sprintf('Name_%d', i)
image = imread(name_of_the_file);
assignin('base',name_of_the_image,image);
clear image name_of_the_file name_of_the_image i
end
Good luck! Christiaan

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!