Plot images with iterative names as subplots

1 view (last 30 days)
I have several images that I have read in as A1 to A43. For instance,
A1= imread('False_Color/20181007.png');
A2= imread('False_Color/20181015.png');
A3= imread('False_Color/20181023.png');
Then I am trying to use a loop to print all 43 images since the names are so similar. I think I am close, but I can't figure out how to remove the quotes so that I can plot imagesc(A3) instead of imagesc('A3') which of course does not work. Please let me know if there is another function I should be using instead of sprintf. Thank you!
This is what I am doing:
for i=1:length(dates_over)
f1=figure(1)
subplot(5,9,i)
imagesc(sprintf('%s%d','A',i)) %I want to loop through so it does imagesc(A1) then imagesc(A2) then imagesc(A3) etc
title(sprintf('%s',dates_over(i)),'fontsize',12) %this works
end

Accepted Answer

Matt J
Matt J on 9 Nov 2020
Edited: Matt J on 9 Nov 2020
First you need to fix the way you read in the images. They should go into the elements of a cell array likse so,
A{1}= imread('False_Color/20181007.png');
A{2}= imread('False_Color/20181015.png');
A{3}= imread('False_Color/20181023.png');
...
A{43}=...
and then you would do,
for i=1:length(dates_over)
f1=figure(1)
subplot(5,9,i)
imagesc(A{i}) %I want to loop through so it does imagesc(A1) then imagesc(A2) then imagesc(A3) etc
title(sprintf('%s',dates_over(i)),'fontsize',12) %this works
end

More Answers (0)

Community Treasure Hunt

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

Start Hunting!