Main Content

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.

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(___,Name,Value) specifies Image properties using one or more Name,Value arguments. Use this option with any of the input argument combinations in the previous syntaxes.

Examples

collapse all

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

fig = uifigure;
im = uiimage(fig);

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.

fig = uifigure;
im = uiimage(fig,"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 specify a URL that opens in a new browser tab when you click the image. Specify a tooltip that appears when you point to the image.

fig = uifigure;
im = uiimage(fig);
im.ImageSource = "ngc6543a.jpg";
im.URL = "https://www.mathworks.com/";
im.Tooltip = "Go to www.mathworks.com";

Image UI component with an image of a nebula in a UI figure window and a tooltip that reads "Go to www.mathworks.com"

Before R2022b: Use the ImageClickedFcn property instead of the URL property and create a callback using a function handle.

im.ImageClickedFcn = @(src,event)web("https://www.mathworks.com/");

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 Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

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 Working with Image Types in MATLAB.

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.

Rectangular image of an icon

Icon in an image component. The icon is centered vertically and scaled to horizontally fill the component. The space above and below the icon is magenta.

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

Rectangular image of an icon

Icon in an image component. The icon fills the component area, and is clipped horizontally.

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.

Rectangular image of an icon

Icon in an image component. The icon is the same size as the original image. The icon is centered vertically and is clipped horizontally.

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'.

Rectangular image of an icon

Icon in an image component. The icon is centered vertically and scaled to horizontally fill the component. The space above and below the icon is magenta.

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'.

Rectangular image of an icon

Icon in an image component. The icon is the same size as the original image. The icon is centered vertically and is clipped horizontally.

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

Rectangular image of an icon

Icon in an image component. The full icon is displayed and fills the image component. The icon is compressed horizontally.

YesYesNoNo

Image hyperlink URL, specified as a character vector or string scalar. When a user clicks the image, the web address specified by the URL opens in a new browser tab. If the user is running the app in a browser using MATLAB Online™ or as a web app, the new tab opens in the current browser. Otherwise, the new tab opens in the default browser on the user's system.

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. If you specify a link to open using the URL property, the callback executes after that link opens.

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 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.

Version History

Introduced in R2019a

expand all

See Also

Functions

Properties

Tools