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 个评论
Geoff Hayes
2020-4-14
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.
BN
2020-4-14
Geoff Hayes
2020-4-14
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.
Geoff Hayes
2020-4-14
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...).
BN
2020-4-14
Geoff Hayes
2020-4-14
Glad to have been able to help, Behzad!
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Scripts 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
