For- loop to plot multiple images on a figure

Hello,
I have 6 images that I want to insert on the one plot at the same time based on the latitude and longitude that specified in a table (namely coordinate). All 6 pictures name like this: filename1.png, filename2.png, filename3.png, ... filename6.png; Which should plot using the information in the coordinate table first, second, third ..., and sixth respectively.
I know how to plot one by one them using this code:
% This code for plot just first image
LowerLeftCornerLon = coordinates.LowerLeftCornerLon(1); % the first image uses the first row of the table, second must use second and so on
UperrRightCornerLon = coordinates.UperrRightCornerLon(1);
LowerLeftCornerLat = coordinates.LowerLeftCornerLat(1);
UperrRightCornerLat = coordinates.UperrRightCornerLat(1);
img1_lon = [LowerLeftCornerLon,UperrRightCornerLon];
img1_lat = [LowerLeftCornerLat,UperrRightCornerLat];
[c, ~, tr] = imread('img1.png');
im = image (img1_lon,img1_lat,flipud(c));
im.AlphaData = tr;
% then use hold on and do the same thing for the second picture but this time reading the second row of coordinate table
But I have some problems with writing a loop in order to plot all of them automatically.
Here is my try:
hold on % plot pictures on the previous plot
% first read all images from dir
if true
srcFiles = dir('F:\university\matlab\mypictures\*.png');
for i = 1 : length(srcFiles)
filename = strcat('F:\university\matlab\mypictures\',srcFiles(i).name);
IMAGES{i} = (filename);
end
end
for i=1:numel(IMAGES)
img_lowlat(i) = coordinates.LowerLeftCornerLat(i);
img_lowlon(i) = coordinates.LowerLeftCornerLon(i);
img_uplat(i) = coordinates.UperrRightCornerLat(i);
img_uplon(i) = coordinates.UperrRightCornerLon(i);
img_lon(i) = [img_lowlon(i),img_uplon(i)];
img_lat(i) = [img_lowlat(i),img_uplat(i)];
hold on
[c, ~, tr] = imread('IMAGES{i}');
im = image (img1_lon,img1_lat,flipud(c));
im.AlphaData = tr;
end
But this not accomplished well and after that, I get this error:
Unable to perform assignment because the indices on the left side are not
compatible with the size of the right side.
Did you know how can I do to fix my for loop and plot all images on the same plot successfully?
Really thank you

7 个评论

Behzad - please copy and paste the full error message to your question. I suspect the error has to do with
img_lon(i) = [img_lowlon(i),img_uplon(i)];
img_lat(i) = [img_lowlat(i),img_uplat(i)];
where you are assigning a two-element array (right-hand side) to a scalar on the left-hand side. Is this the case?
Also, instead of treating the IMAGES as a string (?) like
[c, ~, tr] = imread('IMAGES{i}');
treat/use it like any other array
[c, ~, tr] = imread(IMAGES{i});
I have no idea if the rest of the code will work so please start with this.
Thank you, Sir
I update the code in this way:
hold on % plot pictures on the previous plot
% first read all images from dir
if true
srcFiles = dir('F:\university\matlab\mypictures\*.png');
for i = 1 : length(srcFiles)
filename = strcat('F:\university\matlab\mypictures\',srcFiles(i).name);
IMAGES{i} = (filename);
end
end
for i=1:numel(IMAGES)
img_lowlat(i) = coordinates.LowerLeftCornerLat(i);
img_lowlon(i) = coordinates.LowerLeftCornerLon(i);
img_uplat(i) = coordinates.UperrRightCornerLat(i);
img_uplon(i) = coordinates.UperrRightCornerLon(i);
img_lon = [img_lowlon(i),img_uplon(i)];
img_lat = [img_lowlat(i),img_uplat(i)];
hold on
[c, ~, tr] = imread(IMAGES{i});
im = image (img_lon(i),img_lat(i),flipud(c));
im.AlphaData = tr;
end
Now a new error appears:
Please copy and paste the full error message to this question. There must be an indication as to which line of code this error corresponds to. Else try using the MATLAB debugger to step through the code to see at which point the code is incorrectly accessing an element in an array.
Thank you. I used the debugger and here is the complete error that appears for me.
Index exceeds the number of array elements (2).
Error in Untitled8 (line 26)
im = image (img_lon(i),img_lat(i),flipud(c));
While i must be 6, I saw at this step i = 3 !
that makes sense because img_lon and img_lat are no longer arrays
img_lon = [img_lowlon(i),img_uplon(i)];
img_lat = [img_lowlat(i),img_uplat(i)];
hold on
[c, ~, tr] = imread(IMAGES{i});
im = image (img_lon(i),img_lat(i),flipud(c));
Just replace the last line with
im = image (img_lon,img_lat,flipud(c));
and that should remove that error (though something else might occur...).
Dear Geoff Hayes,
Thank you so much, it is fixed now and worked very well. I'll click on accept the answer if you post it as an answer.
It is really helpful. Thanks again
Best Regards
Glad to have been able to help, Behzad!

请先登录,再进行评论。

 采纳的回答

The original error was concerned with
img_lon(i) = [img_lowlon(i),img_uplon(i)];
img_lat(i) = [img_lowlat(i),img_uplat(i)];
where a two-element array (right-hand side) was being assigned to a scalar on the left-hand side. This was replaced with
img_lon = [img_lowlon(i),img_uplon(i)];
img_lat = [img_lowlat(i),img_uplat(i)];
hold on
[c, ~, tr] = imread(IMAGES{i});
im = image (img_lon,img_lat,flipud(c));

更多回答(0 个)

类别

帮助中心File Exchange 中查找有关 Scripts 的更多信息

产品

版本

R2020a

标签

Community Treasure Hunt

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

Start Hunting!

Translated by