Interface for customizing object display within containers
This class provides an interface for customizing the way MATLAB® represents objects using compact display. Compact display refers to a scenario in which an object array is held within a container (such as a structure, cell array, or table). For example, when an object array is held in a field of a structure, MATLAB can display the array only in a single line and within the limited character width available to that field.
To add the custom compact display functionality to your class, derive it from
classdef MyClass < matlab.mixin.CustomCompactDisplayProvider ... end
You can use
matlab.mixin.CustomCompactDisplayProvider to customize compact
display for two display layouts:
Single-line layout — The object array must be displayed in a single row. MATLAB uses this display layout when the object array is contained within a structure or cell array, or as a property of a MATLAB object.
Columnar layout — The object array must be displayed in multiple rows as part of column-oriented or tabular data. MATLAB uses this display layout when the object array is contained within a table variable.
For information on class attributes, see Class Attributes.
|Customize single-line compact display representation of object array|
|Customize columnar compact display representation of object array|
Customize Compact Display
To display custom information about the objects of your class when
they are held within a container, derive your class from the
matlab.mixin.CustomCompactDisplayProvider interface and override select
methods of the interface.
In your current folder, create the
Weekdays enumeration class by
matlab.mixin.CustomCompactDisplayProvider. To customize the
compact display for single-line and columnar layouts, override the
compactRepresentationForColumn methods, respectively:
Single-line layout — Fit as many elements of the object array within the available character width as possible. Also, add an annotation if the array includes weekend days. To customize the compact display for single-line layout, place a call to the
widthConstrainedDataRepresentationutility method within
Columnar layout — Fit all elements of the object array within the available character width, or else use the array dimensions and class name. Also, add an annotation for each row of the object array that includes weekend days. To customize the compact display for columnar layout, place a call to the
fullDataRepresentationutility method within
classdef WeekDays < matlab.mixin.CustomCompactDisplayProvider enumeration Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday end methods function rep = compactRepresentationForSingleLine(obj,displayConfiguration,width) % Fit as many array elements in the available space as possible [rep,~] = widthConstrainedDataRepresentation(obj,displayConfiguration, ... width,Annotation=annotation(obj)); end function rep = compactRepresentationForColumn(obj,displayConfiguration,~) % Fit all array elements in the available space, or else use % the array dimensions and class name rep = fullDataRepresentation(obj,displayConfiguration, ... Annotation=annotation(obj)); end function res = annotation(obj) % Construct annotation as a column vector of strings numRows = size(obj,1); res = strings(numRows,1); for i = 1:numRows % Add text for each row that includes weekend days currentRow = obj(i,:); if any(currentRow == WeekDays.Saturday) || any(currentRow == WeekDays.Sunday) res(i) = "Includes Weekends"; end end end end end
In the Command Window, create a structure with a field that contains an array of a
Weekdays objects. MATLAB displays all the array elements in a single line. Additionally, because
the array includes the enumeration member
MATLAB displays an annotation.
s = struct("FreeLunchDays",[WeekDays.Monday WeekDays.Wednesday WeekDays.Friday WeekDays.Saturday])
s = struct with fields: FreeLunchDays: [Monday Wednesday Friday Saturday] (Includes Weekends)
Weekdays array with many elements, so that they
cannot all be displayed in a single line. When you assign this array to
s.FreeLunchDays, MATLAB displays as many leading array elements as possible and uses an ellipsis
symbol to represent the omitted elements.
days = repmat(WeekDays.Friday,1,52); s.FreeLunchDays = days
s = struct with fields: FreeLunchDays: [Friday Friday Friday Friday Friday Friday Friday Friday … ]
Now, test the custom compact display of
WeekDays objects for
columnar layout. Create a table
T that contains a
WeekDays array comprising a few elements. Because the available
character width is large enough, MATLAB displays all the array elements. Additionally, because the second row of
the array includes the enumeration member
MATLAB displays an annotation for that row.
Location = ["Boston"; "New York"]; FreeLunchDays = [WeekDays.Wednesday WeekDays.Friday; WeekDays.Thursday WeekDays.Saturday]; T = table(Location,FreeLunchDays)
T = 2×2 table Location FreeLunchDays __________ __________________________________________ "Boston" Wednesday Friday "New York" Thursday Saturday (Includes Weekends)
FreeLunchDays variable using a
WeekDays array with many elements. Because MATLAB can no longer display all the array elements within the available
character width, it uses the array dimensions and class name.
T.FreeLunchDays = repmat(WeekDays.Friday,2,52)
T = 2×2 table Location FreeLunchDays __________ _____________ "Boston" 1×52 WeekDays "New York" 1×52 WeekDays
Version HistoryIntroduced in R2021b
R2022b: Live Editor, Variables Editor, and Workspace Panel Honor Compact Display
As of R2022b, the rules implemented by
honored by the Live Editor, the Variables editor in MATLAB
Online™, and the single-line
'Value' field in the Workspace panel