How to insert a saved .png figure into a subplot which is being created in a for loop?

조회 수: 44 (최근 30일)
Hello Friends,
I have the following code
for k = 1:2
subplot(1,3,k)
plot(randi(10,k));
end
I want to insert a saved .png (I prefer .png, but if it is easier for .fig, it is also ok) into this subplot which is being created in for loop at position subplot(1,3,1). This way, the inserted figure will come at the first position moving other two subfigures to the next available positions.
I will appreciate any advise!

채택된 답변

Walter Roberson
Walter Roberson 2016년 8월 28일
subplot(1,3,1)
image( imread('ThePNGFile.png') );
for k = 1:2
subplot(1,3,k+1)
plot(randi(10,k));
end
See also the XData and YData properties of image() and imshow() if you want to draw an image at a particular location in an axes that has other graphics.
  댓글 수: 4
hello_world
hello_world 2016년 8월 28일
편집: hello_world 2016년 8월 28일
Thanks. Could you please show an example of extracting something from the .fig and put it into an axes?
Adding axis image shrinks the .png image to it's normal size while other two images are quite big in height which makes it look odd.
Walter Roberson
Walter Roberson 2016년 8월 28일
ax = subplot(1, 3, 1);
fig = openfig('MyFile.fig', 'visible', 'off');
imh = findobj(fig, 'type', 'image');
copyobj(imh, ax);
delete(fig);

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2016년 8월 28일
Try this:
for k = 1:3
subplot(1,3,k)
% Insert image into subplot.
filename = whatever......
thisImage = imread(filename);
imshow(thisImage);
hold on;
% Now draw the plot over it.
plot(x, y);
drawnow;
end
  댓글 수: 1
hello_world
hello_world 2016년 8월 28일
편집: hello_world 2016년 8월 28일
Thanks! What are x and y please?
Using your code as follows gives the same .png subfigure in all subplots:
for k = 1:3
subplot(1,3,k)
% Insert image into subplot.
filename = 'myFig.png';
thisImage = imread(filename);
imshow(thisImage);
hold on;
% Now draw the plot over it.
plot(randi(10,k));
%plot(x, y);
drawnow;
end
It does not insert anything from plot(randi(10,k)); for subplot position 2 and 3.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by