viewer2d
Description
creates a
viewer = viewer2dViewer object in a new figure window that is configured for 2-D image
display. Use viewer to query and modify properties of the
Viewer object after you create the object. For a list of properties, see
Viewer Properties.
creates a viewer = viewer2d(___,Name=Value)Viewer object and sets properties of the object using one or more
name-value arguments in addition to any of the input arguments from previous
syntaxes.
For example, viewer2d(BackgroundColor="green") creates a
Viewer object in a new web figure with a green background.
Examples
Create a viewer configured for 2-D image display. The viewer2d function creates a new Viewer object with default property values suitable for 2-D images.
viewer = viewer2d
viewer =
Viewer with properties:
Parent: [1×1 Figure]
Position: [1 1 560 420]
Children: [0×0 GraphicsPlaceholder]
BackgroundColor: [1 1 1]
BackgroundGradient: off
GradientColor: [1 1 1]
OrientationAxes: off
ClippingPlanes: [0×4 single]
Lighting: on
LightPosition: [0 0 0]
CameraPosition: [1 1 1]
CameraTarget: [0 0 0]
CameraUpVector: [0 0 1]
CameraZoom: 1
Show all properties
Add an image to the viewer using the imageshow function with the Parent name-value argument specified as the handle to the Viewer object.
im = imageshow("peppers.png",Parent=viewer);
Optionally, show the scale bar.
viewer.ScaleBar = "on";
Create a grid layout with one row and two columns. The layout opens in a new figure outside of the Live Editor.
h = uifigure(Position=[100 100 1000 420]);
g = uigridlayout(h,ColumnWidth={'1x','1x'},RowHeight={'fit','1x'});Create two viewers to display 2-D images.
viewerLeft = viewer2d(g); viewerRight = viewer2d(g);
Specify the title for each grid element.
labelLeft = uilabel(g,Text="Spread Out",HorizontalAlignment="center"); labelRight = uilabel(g,Text="Close Together",HorizontalAlignment="center");
Specify the layout options for the grid. Nest the left viewer and its label in the first column. Nest the right viewer and its label in the second column.
labelLeft.Layout.Row = 1; labelLeft.Layout.Column = 1; labelRight.Layout.Row = 1; labelRight.Layout.Column = 2; viewerLeft.Layout.Row = 2; viewerLeft.Layout.Column = 1; viewerRight.Layout.Row = 2; viewerRight.Layout.Column = 2;
Display two images of hands side-by-side in the UI grid layout.
imageshow("hands1.jpg",Parent=viewerLeft); imageshow("hands2.jpg",Parent=viewerRight);
This image shows how the grid layout appears in the figure window, which is displayed outside of the Live Editor.

Input Arguments
Parent of the viewer, specified as a Figure object created using
the uifigure function, or a
Panel, GridLayout, or Tab object
whose parent is a figure created using the uifigure function. You
can use the uipanel, uigridlayout, and uitab functions to create the corresponding objects.
Tip
If you want to build an app in App Designer, or you want to add and position other
UI components in a figure with the 2-D viewer, the recommended parent is a
GridLayout object.
When the parent is a GridLayout object, you can adjust the layout
of the 2-D viewer using the Layout property.
Example: parent = uifigure; viewer = viewer2d(parent); creates a
parent figure using the uifigure function.
Example: f = uifigure; parent = uipanel(f); viewer =
viewer2d(parent); creates a parent Panel object whose
parent is a figure created using the uifigure
function.
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.
Example: viewer2d(BackgroundColor="green",BackgroundGradient=false)
creates a Viewer object in a new figure with a green background and no
background gradient.
Note
The properties listed here are only a subset. For a full list, see Viewer Properties.
Color of the background, specified as an RGB triplet, a hexadecimal color code, a
color name, or a short color name. When you select light mode in MATLAB®, the default color is [0.9608 0.9608 0.9608]. When
you select dark mode in MATLAB, the default color is [0.1 0.1 0.1].
For a custom color, specify an RGB triplet or a hexadecimal color code.
An RGB triplet is a three-element row vector whose elements specify the intensities of the red, green, and blue components of the color. The intensities must be in the range
[0,1], for example,[0.4 0.6 0.7].A hexadecimal color code is a string scalar or character vector that starts with a hash symbol (
#) followed by three or six hexadecimal digits, which can range from0toF. The values are not case sensitive. Therefore, the color codes"#FF8800","#ff8800","#F80", and"#f80"are equivalent.
Alternatively, you can specify some common colors by name. This table lists the named color options, the equivalent RGB triplets, and the hexadecimal color codes.
| Color Name | Short Name | RGB Triplet | Hexadecimal Color Code | Appearance |
|---|---|---|---|---|
"red" | "r" | [1 0 0] | "#FF0000" |
|
"green" | "g" | [0 1 0] | "#00FF00" |
|
"blue" | "b" | [0 0 1] | "#0000FF" |
|
"cyan"
| "c" | [0 1 1] | "#00FFFF" |
|
"magenta" | "m" | [1 0 1] | "#FF00FF" |
|
"yellow" | "y" | [1 1 0] | "#FFFF00" |
|
"black" | "k" | [0 0 0] | "#000000" |
|
"white" | "w" | [1 1 1] | "#FFFFFF" |
|
This table lists the default color palettes for plots in the light and dark themes.
| Palette | Palette Colors |
|---|---|
Before R2025a: Most plots use these colors by default. |
|
|
|
You can get the RGB triplets and hexadecimal color codes for these palettes using the
orderedcolors
and rgb2hex
functions. For example, get the RGB triplets for the "gem" palette and
convert them to hexadecimal color
codes.
RGB = orderedcolors("gem");
H = rgb2hex(RGB);Before R2023b: Get the RGB triplets using RGB =
get(groot,"FactoryAxesColorOrder").
Example: BackgroundColor="r"
Example: BackgroundColor="green"
Example: BackgroundColor=[0 0.4470 0.7410]
Example: BackgroundColor="#FF8800"
Since R2025a
Title for the viewer, specified as a string scalar or character vector. The title appears in the top center of the viewer window.
Spatial units for the viewer, specified as a string scalar. Use this value to change the units label associated with viewer display tools, such as the scale bar and point and line annotations.
Display scale bar in the lower-right corner, specified as "on" or "off", or as a numeric or logical
1 (true) or 0
(false). A value of "on" is equivalent to
true, and "off" is equivalent to
false. The value is stored as an on/off logical value of type OnOffSwitchState.
Control the style of the scale bar by using the ScaleBarStyle
value.
Since R2025a
Style of the scale bar, specified as one of the strings in the table. Control the
visibility of the scale bar by using the ScaleBar
value.
| Value | Description |
|---|---|
"zoom" | Display a scale bar with the zoom level of the image. The zoom level is
based on a combination of available screen size, the image size, and the
viewer camera zoom level. A scale bar zoom level of
100% indicates that one screen pixel displays one
data pixel, or one unit in world coordinates if you specify the
Transformation property of the Image
object. |
"measure" | Display a measurement scale bar with units specified by the
SpatialUnits value. |
Output Arguments
2-D viewer, returned as a Viewer object configured for 2-D image
display. When you create a Viewer by using the
viewer2d function, the function sets certain properties to
support 2-D visualization. For details about which properties have different defaults
when created by viewer2d instead of viewer3d,
see Viewer Properties.
More About
The 2-D image viewer provides these tools for exploring and modifying the image display.
| Tool | Description | Representative Image |
|---|---|---|
| View pixel information | The viewer displays information about the pixel beneath the pointer. The display includes a zoomed in view of the pixel neighborhood, pixel coordinates, grayscale intensity or RGB color values, and the overlay value, if one is specified. To interactively toggle the visibility of the display, open the context menu by right-clicking inside the viewer, and select or clear Display info. Alternatively, you can toggle the visibility programmatically by setting the You can copy or export the pixel information from the viewer context menu. To save individual values to the clipboard, select Copy intensity, Copy color, or Copy location. To save the pixel information as a structure in the workspace, select Export info to workspace. In the Export to workspace dialog box, specify a name for the structure and select OK. |
|
| Copy or save screenshot | To save a screenshot of the displayed image without the toolbar or scale bar, right-click to open the viewer context menu, and select Copy screenshot to clipboard or Save screenshot to file. |
|
| Adjust the display range of grayscale images | From the viewer toolbar, select the adjust window level icon The viewer hides the window level tool when displaying binary or RGB images. | |
| Annotate Regions of Interest (ROIs) | From the viewer toolbar, click the draw annotations icon Click and drag in the image to draw an ROI annotation.
Each shape displays a label, such as the coordinates of a point or the
length of a line, with the units label specified by the Right-click the ROI to open a context menu from which you can edit the text label, remove the ROI, or export the line to the workspace as an object. |
To receive notification from the
Viewer object when certain events happen, set up
listeners for these events. You can specify a callback function
that executes when one of these events occurs. When the Viewer object
notifies your application through the listener, it returns data specific to the event. Look
at the event class for the specific event to see what is returned.
| Event Name | Trigger | Event Data | Event Attributes |
|---|---|---|---|
AnnotationAdded | An ROI annotation has been interactively added to the viewer. This event does not execute if an annotation is programmatically added. | images.ui.graphics.events.AnnotationAddedEventData |
|
AnnotationEdited | The label of an ROI annotation has been interactively edited. This event does not execute if an annotation label is programmatically edited. | images.ui.graphics.events.AnnotationEditedEventData |
|
AnnotationMoved | An ROI annotation has been interactively moved within the viewer. This event does not execute if an annotation is programmatically moved. | images.ui.graphics.events.AnnotationMovedEventData |
|
AnnotationRemoved | An ROI annotation has been interactively removed from the viewer. This event does not execute if an annotation is programmatically removed. | images.ui.graphics.events.AnnotationRemovedEventData |
|
CameraMoving | The camera is being interactively moved. This event does not execute if the camera is programmatically moved. | images.ui.graphics.events.CameraMovedEventData |
|
CameraMoved | The camera stops being interactively moved. This event does not execute if the camera is programmatically moved. | images.ui.graphics.events.CameraMovedEventData |
|
ClippingPlanesChanging | A global clipping plane is being interactively moved. This event does not execute if the clipping plane is programmatically moved. | images.ui.graphics.events.ClippingPlanesChangedEventData |
|
ClippingPlanesChanged | A global clipping plane stops being interactively moved. This event does not execute if the clipping plane is programmatically moved. | images.ui.graphics.events.ClippingPlanesChangedEventData |
|
CropRegionChanged | The crop region stops being interactively changed. This event does not execute if the crop region is programmatically changed. | images.ui.graphics.events.CropRegionChangedEventData |
|
ViewerRefreshed | The viewer has been refreshed. You can refresh a viewer either by reparenting the viewer or by modifying the ancestor hierarchy of the parent object. When you refresh a viewer, the viewer is cleared and no longer contains any child objects. To recreate the scene, you can listen to this event and reconstruct any objects that were parented to the viewer. | event.EventData |
|
Tips
To clear all objects from the scene and delete all data containers previously added to the 2-D viewer
viewer, use the command:clear(viewer)
To delete all objects in the scene and the 2-D viewer
viewer, use the command:delete(viewer)
Version History
Introduced in R2024bThe viewer supports new annotations objects, and new options for creating annotations,
including the existing Point and Line objects.
The
Annotationsproperty now supportsRectangle,Circle,Polygon,Polyline,Angle,Freehand, andEllipseobjects.You can add
Point,Line,Angle,Rectangle,Polygon, andCircleannotations from the 2-D viewer toolbar. The 3-D viewer toolbar supports onlyLineandPointannotations.Create any ROI shape by using the new
uidrawfunction. For example, the syntaxuidraw(viewer,"circle")enables you to interactively draw a circle ROI in the specified viewer, and adds the ROI object to theAnnotationsproperty of that viewer.
Using new camera properties, you can:
Control the behavior of the rotate, zoom, and pan interactions by specifying the new
CameraInteractionStyleproperty. For example, select a style that rotates the view about the pointer location instead of the center of the volume, or that uses a dolly zoom to simulate a first-person perspective.View the scene using a perspective or orthographic camera by specifying the new
CameraProjectionproperty.Control the field of view by specifying the new
CameraViewAngleproperty.Define the visible area in view of the camera by specifying the new
CameraViewportproperty.
The 2-D viewer now supports:
Adding a title. Add a title by specifying the new
Titleproperty.Adding a measurement scale bar by specifying the new
ScaleBarStyleproperty as"measure".Displaying information about the pixel beneath the pointer. You can interactively copy pixel coordinates and intensity or color values to the clipboard, or export pixel information to the workspace from the viewer context menu. Toggle the visibility of the pixel information display from the viewer context menu or by using the new
DisplayInfoproperty.
The viewer toolbar is now always visible by default, regardless of whether the pointer
is inside or outside the viewer window. To preserve the behavior of previous releases,
specify the Toolbar property as "hover", which hides
the toolbar when you move the pointer outside the viewer window.
The viewer scale bar is now off by default. To make the scale bar visible, set the
ScaleBar property as "on".
See Also
Viewer Properties | linkviewers | imageshow | viewer3d | viewangle | uidraw | uipaint
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)