Create image component
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(
specifies
Name,Value
)Image
property values using one or more name-value pair
arguments.
im = uiimage(
specifies the parent container and one or more property values.parent
,Name,Value
)
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 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 an external URL in your system 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 fig = uifigure('Visible','off'); fig.Position(3:4) = [333 239]; im = uiimage(fig); 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 fig.Visible = 'on'; end
parent
— Parent containerFigure
object (default) | Panel
object | Tab
object | ButtonGroup
object | GridLayout
objectParent 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.
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
.
im = uiimage('ScaleMethod','none')
Note
The properties listed here are only a subset. For a complete list, see Image Properties.
'ImageSource'
— Image source or filem
-by-n
-by-3 truecolor image arrayImage 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';
'ScaleMethod'
— Image scaling method'fit'
(default) | 'fill'
| 'none'
| 'scaledown'
| 'scaleup'
| 'stretch'
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.
Value | Description | Example | Scales Up | Scales Down | Maintains Aspect Ratio | Clips Image | |
---|---|---|---|---|---|---|---|
Original Image | Rendered Image | ||||||
'fit' | Scales in any direction to display the image within the component area, and maintains aspect ratio without clipping. |
|
| Yes | Yes | Yes | No |
'fill' | Scales in any direction to fill the component area, maintaining aspect ratio and clipping if necessary. |
|
| Yes | Yes | Yes | Yes |
'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. |
|
| No | No | Yes | Yes |
'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 |
|
| No | Yes | Yes | No |
'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 |
|
| Yes | No | Yes | Yes |
'stretch' | Scales in any direction to fill the component area, without maintaining the aspect ratio and without clipping. |
|
| Yes | Yes | No | No |
'ImageClickedFcn'
— Image clicked callbackImage 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.
Property | Value |
---|---|
EventName | 'ImageClicked' |
Source | Component executing the callback |
For more information about writing callbacks, see Write Callbacks in App Designer.
'Position'
— Location and size of image component[100 100 100 100]
(default) | [left bottom width height]
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.
Element | Description |
---|---|
left | Distance from the inner left edge of the parent container to the outer left edge of the image component |
bottom | Distance from the inner bottom edge of the parent container to the outer bottom edge of the image component |
width | Distance between the right and left outer edges of the image component |
height | Distance 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.
You have a modified version of this example. Do you want to open this example with your edits?