uiimage

Create image component

Description

im = uiimage creates an image component in a new figure and returns the Image object. MATLAB® calls the uifigure function to create the new figure. Use uiimage to display a picture, icon, or logo in your app.

im = uiimage(Name,Value) specifies Image property values using one or more name-value pair arguments.

example

im = uiimage(parent) creates an image component in the specified parent container. The parent can be a figure created using the uifigure function, or one of its child containers.

example

im = uiimage(parent,Name,Value) specifies the parent container and one or more property values.

Examples

collapse all

Create an image component within a figure. The default image displays.

f = uifigure;
im = uiimage(f);

Now, add a picture to the image component.

im.ImageSource = 'peppers.png';

Create an image component that displays an animated GIF using the actual size of the image.

f = uifigure;
im = uiimage(f,'ImageSource','questions.gif');
im.ScaleMethod = 'none';

Now, scale the image so that it fits within the default component area, preserving aspect ratio and without clipping. Then, apply a black background to create the appearance of letterboxing (black bars above and below the image).

im.ScaleMethod = 'scaledown';
im.BackgroundColor = 'black';

Create an image and a callback that executes when the image is clicked. In this case, the image opens the MathWorks® website.

This program file, called imagetoURL.m, shows you how to:

  • Create an image component with an ImageClickedFcn callback.

  • Use the web function within the callback to open a URL in the MATLAB® browser.

  • Create a tooltip that appears when you hover over the image.

When you run the program file, click the image to open the MathWorks® website.

function imagetoURL
f = uifigure('Visible','off');
f.Position(3:4) = [333 239];

im = uiimage(f);
im.Position = [20 120 100 100];
im.ImageSource = 'membrane.png';
im.ImageClickedFcn = @ImageClicked;
im.Tooltip = 'Go to www.mathworks.com';

    function ImageClicked(src,event)
        url = 'https://www.mathworks.com/';
        web(url);
    end

f.Visible = 'on';
end

Input Arguments

collapse all

Parent container, specified as a Figure object created using the uifigure function, or one of its child containers: Tab, Panel, ButtonGroup, or GridLayout. If you do not specify a parent container, MATLAB calls the uifigure function to create a new Figure object that serves as the parent container.

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: im = uiimage('ScaleMethod','none')

Note

The properties listed here are only a subset. For a complete list, see Image Properties.

Image source or file, specified as a file path or an m-by-n-by-3 truecolor image array. Supported image formats include JPEG, PNG, GIF, SVG, or m-by-n-by-3 truecolor image array.

For more information on truecolor image arrays, see Image Types.

Example: im = uiimage('ImageSource','peppers.png');

Example: im.ImageSource = 'C:\TEMP\ngc6543a.jpg';

Image scaling method, specified as one of the values listed in the table. Use this name-value pair argument to specify how you want your image to render within the component area.

The table also demonstrates each scale method with an example image. In the rendered image examples, the BackgroundColor property of the image component has been set to 'magenta'. The scaling behavior of SVG image files may vary based on how the file is defined.

ValueDescriptionExampleScales UpScales DownMaintains Aspect RatioClips Image
Original ImageRendered Image
'fit'Scales in any direction to display the image within the component area, and maintains aspect ratio without clipping.

YesYesYesNo
'fill'Scales in any direction to fill the component area, maintaining aspect ratio and clipping if necessary.

YesYesYesYes
'none'Uses the actual size of the image and maintains aspect ratio. If the component area is smaller than the image, the image is clipped.

NoNoYesYes
'scaledown'

Scales down and maintains aspect ratio without clipping.


If the original image is larger than the component area, the image scales down and renders as if the ScaleMethod was set to 'fit'. If the original image is smaller than the component area, the image does not scale down and renders as if the ScaleMethod was set to 'none'.

NoYesYesNo
'scaleup'

Scales up and maintains aspect ratio with clipping.


If the original image is smaller than the component area, the image scales up and renders as if the ScaleMethod was set to 'fit'. If the original image is larger than the component area, the image does not scale up and renders as if the ScaleMethod was set to 'none'.

YesNoYesYes
'stretch'Scales in any direction to fill the component area, without maintaining the aspect ratio and without clipping.

YesYesNoNo

Image clicked callback, specified as one of these values:

  • A function handle.

  • A cell array in which the first element is a function handle. Subsequent elements in the cell array are the arguments to pass to the callback function.

  • A character vector containing a valid MATLAB expression (not recommended). MATLAB evaluates this expression in the base workspace.

This callback executes when the user clicks the image in the app.

This callback function can access specific information about the user's interaction with the image. MATLAB passes this information in an ImageClickedData object as the second argument to your callback function. In App Designer, the argument is called event. You can query the object properties using dot notation. For example, event.Source returns the Image object that the user is interacting with to trigger the callback. The ImageClickedData object is not available to callback functions specified as character vectors.

The following table lists the properties of the ImageClickedData object.

PropertyValue
EventName'ImageClicked'
SourceComponent executing the callback

For more information about writing callbacks, see Write Callbacks in App Designer.

Location and size of image component relative to the parent, specified as a four element vector of the form [left bottom width height]. This table describes each element in the vector.

ElementDescription
leftDistance from the inner left edge of the parent container to the outer left edge of the image component
bottomDistance from the inner bottom edge of the parent container to the outer bottom edge of the image component
widthDistance between the right and left outer edges of the image component
heightDistance between the top and bottom outer edges of the image component

The Position values are relative to the drawable area of the parent container. The drawable area is the area inside the borders of the container and does not include the area occupied by decorations such as a menu bar or title.

All measurements are in pixel units.

Tips

  • For image analysis and processing, see graphics functions image and imshow.

See Also

Functions

Properties

Introduced in R2019a