Reputation: 245
I just want to iterate through an image array one by one but I can't get it working, I get the errors below. I can show single images via imshow (a), but not iterate through an array.
Error using getImageFromFile (line 12) Cannot find the specified file: "a".
Error in imageDisplayParseInputs (line 74) [common_args.CData,common_args.Map] = ...
Error in imshow (line 198) [common_args,specific_args] = ...
Code
% Images
a = 'redsquare.bmp';
b = 'bluesquare.bmp';
c = 'greysquare.bmp';
d = 'yellowsquare.bmp';
% Array to display
t = [a b c d];
% Loop to display
for n=1:length(t)
imshow(t(n))
end
Thanks.
Upvotes: 1
Views: 1532
Reputation: 26069
You have several bugs in the code as it is. First, verify that the directory where the images are located is in Matlab's path , or try adding the full path of the images locations. I'd recommend to use a cell array to contain all the file names. The way you've done it in the question is to concatenate all strings to one long one, that is
t =
redsquare.bmpbluesquare.bmpgreysquare.bmpyellowsquare.bmp
So in the code change t
to:
t = {a, b, c, d}; % note the curly brackets
In the loop, you need first to read the image to an array before showing it, therefor add an imread
line. After reading the file onto an array (lets called that array im
), you can use imshow
to plot it. All in all, the code in the loop should be:
for n=1:numel(t)
im=imread(t{n});
imshow(im);
end
Again, note that I used curly brackets {}
for the imread
line, that is the way to extract the cell element content.
Upvotes: 2
Reputation: 1911
it is a while since i have done matlab so i am struggling to remember the syntax, however i am pretty sure that your problem is that a bitmap is made up of a 2 dimensional array, which you are putting into another array.
Therefore when you do t(1), your not getting the entire bitmap, just a single element.
Firstly try asking matlab for the shape of t. The syntax eludes me it has been some time. Then use the required syntax to extract the required 2-d matrix.
Hope this helps
Upvotes: 0