Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
adding image to figure

Subject: adding image to figure

From: Emily

Date: 29 Mar, 2010 21:10:24

Message: 1 of 5

I think this is pretty easy, but I've been through my entire volume of Matlab Graphics documentation, and can't find how to do this.

I need to add a small image to the corner of each of a series of four plots. I have the image as a JPG. What is the easiest way to do this?

Thanks much,
Emily

Subject: adding image to figure

From: Rob Campbell

Date: 29 Mar, 2010 22:03:09

Message: 2 of 5

"Emily " <tdd214dontspam@gmail.com> wrote in message <hor500$t5k$1@fred.mathworks.com>...
> I think this is pretty easy, but I've been through my entire volume of Matlab Graphics documentation, and can't find how to do this.
>
> I need to add a small image to the corner of each of a series of four plots. I have the image as a JPG. What is the easiest way to do this?
>
The easiest way I know of is with the image processing toolbox. However, it sounds like you may not have this. In the even that you do:

im=imread('t.jpg');
clf
plot(randn(1,100))
axes('position',[0,0.9,0.1,0.1])
imshow(im)

The two "im" commands are from the image processing toolbox. There is a function on the FEX which you could substitute for imread (not tried it myself):
http://www.mathworks.fr/matlabcentral/fileexchange/10476-jpeg-codec
Search on the FEX if that doesn't work.

Subject: adding image to figure

From: Walter Roberson

Date: 29 Mar, 2010 22:04:24

Message: 3 of 5

Emily wrote:

> I need to add a small image to the corner of each of a series of four
> plots. I have the image as a JPG. What is the easiest way to do this?

Note this part of the documentation on image():


image(x,y,C), where x and y are two-element vectors, specifies the range of
the x- and y-axis labels, but produces the same image as image(C). This can be
useful, for example, if you want the axis tick labels to correspond to real
physical dimensions represented by the image. If x(1) > x(2) or y(1) > y(2),
the image is flipped left-right or up-down, respectively. It can also be
useful when you want to place the image within a set of axes already created.
In this case, use hold on with the current figure and enter xand yvalues
corresponding to the corners of the desired image location. The image is
stretched and oriented as applicable



Thus, to position an image at an arbitrary location on the axis, use an
image() call that specifies the coordinates of the corners it is to be placed at.

imagesc() also supports this calling sequence.

Subject: adding image to figure

From: ImageAnalyst

Date: 29 Mar, 2010 23:46:01

Message: 4 of 5

That's what I thought of first too Walter but I couldn't get it to
work. Maybe I did something wrong (my code is below). I had to
create a new axes and place it on top of the other axes. My code
showing the non-working method, and my two-axis method follows:
(Be sure to fix any line breaks introduced by the newsreader.)


clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear all; % Erase all existing variables.
workspace; % Make sure the workspace panel is showing.

% Make up some sample data and plot it.
numberOfDataPoints = 200;
sampleData = 100*rand(1,numberOfDataPoints);
plot(sampleData);
xlim([1, numberOfDataPoints]);
hold on;

% Read in our image.
grayImage = imread('eight.tif');
% Try to display it in the same axes.
image(20,40, grayImage);
colormap('gray');
title('Not working', 'FontSize', 20);
set(gcf, 'Position', get(0,'Screensize')); % Maximize figure.
message = sprintf('Observe that this did not exactly work.\nThe image
is just a white square covering some of the data.\nLet us try
again...');
uiwait(msgbox(message));
hold off;

% Let's try again.
% Plot the original data
plot(sampleData);
xlim([1, numberOfDataPoints]);
title('Working - Observe logo in upper right corner', 'FontSize', 20);
% Make a new axes for our grayscale image
% that we will put on top of the existing figure.
axes1Position = get(gca, 'Position');
logoSizeX = 0.1;
logoSizeY = 0.1;
% Position the logo in the upper right.
x1 = axes1Position(1) + axes1Position(3) - logoSizeX;
y1 = axes1Position(2) + axes1Position(4) - logoSizeY;
hAxis2 = axes('Position', [x1 y1 logoSizeX logoSizeY]);
axis off; % Turn off tick marks, etc.
imshow(grayImage);

Subject: adding image to figure

From: Walter Roberson

Date: 31 Mar, 2010 18:34:22

Message: 5 of 5

ImageAnalyst wrote:
> That's what I thought of first too Walter but I couldn't get it to
> work. Maybe I did something wrong (my code is below). I had to
> create a new axes and place it on top of the other axes. My code
> showing the non-working method, and my two-axis method follows:
> (Be sure to fix any line breaks introduced by the newsreader.)

Using image with position parameters worked fine for me in 2008b, provided I
remembered the 'hold on'.


I did, though, discover an oddity: if you use image without any parameters,
then the default image (of the boy) is added to whatever graphic is present,
even if you do not have 'hold on'.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us