Build compact display representation based on available character width
builds a compact display representation of the object array
num] = widthConstrainedDataRepresentation(
obj that is
adjusted to the available character width, using the string converter method defined by the
obj. The method returns the representation and number of
elements included in the representation in
num, respectively. To construct a representation suitable for display
within the container, the method uses the current display context as described in
widthConstrainedDataRepresentation tries to build a plain-text
representation by fitting as many elements of
obj within the available
character width as possible. Depending on the value of
representation might include the data in all or a portion of
width is not large enough to fit any array elements, then
the method returns a representation using the array dimensions and class name.
obj — Object array to display
Object array to display, specified as an object array of a class derived from
displayConfiguration — Description of current display context
Description of the current display context, specified as a
width — Available character width
positive integer scalar
Available character width to display the object array, specified as a positive integer scalar.
Specify optional pairs of arguments as
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.
rep = widthConstrainedDataRepresentation(obj,displayConfiguration,width,Annotation="My Object")builds a width-constrained representation of
objthat includes the specified annotation.
StringArray — Textual representation of data in object array
Textual representation of the data in the object array, specified as a string array
with the same number of rows as
obj. When you specify this
argument, the method uses the specified string array instead of calling the string
converter method of the class on the object array.
Use this argument when the class of the object array does not implement a string converter method or when the converter method does not create a string array suitable for use in a compact display scenario.
Annotation — Descriptive comment about object array
N-by-1 string array
Descriptive comment about the object array, specified as an N-by-1 string array, where
N is the number of rows in
obj. The method appends the specified
annotation to the text representing the data in the object array.
MimimumElementsToDisplay — Minimum number of array elements to include in representation
1 (default) | positive integer scalar
TruncateScalarObject — Whether to truncate string scalars
false (default) |
Whether to truncate string scalars, specified as
true. Use this argument when
represented as a string scalar (single-line layout) or a column vector of string
scalars (columnar layout), and you want to make sure that the string scalars are
displayed by the container of
obj. When the value is
true, the method truncates the string scalars, if necessary, so
that they fit within the available width. It replaces the removed characters from the
tails of the string scalars with an ellipsis symbol.
widthConstrainedDataRepresentation does not truncate
string scalars. If the value is
false and a string scalar does not
fit within the available width, the method returns a
DimensionsAndClassNameRepresentation object with
set to zero.
rep — Compact display representation of object array
matlab.display.PlainTextRepresentation object |
Compact display representation of the object array, returned as a
By default, the method tries to represent
obj as a
PlainTextRepresentation object. It returns a
DimensionsAndClassNameRepresentation object only in these situations:
The available character width is not large enough to display any of the data in
objis an empty value.
objmust use the single-line display layout, but it is not a row vector.
objmust use the columnar display layout, but it is not a matrix.
num — Number of elements included in compact display representation
nonnegative integer scalar
Number of elements included in the compact display representation, returned as a nonnegative integer scalar.
Customize Compact Display to Use Maximum Number of Array Elements
Customize the compact display of your object array by fitting as many array elements within the available character width as possible, and also specifying an annotation that depends on the contents of the array.
In your current folder, create the
Weekdays enumeration class by
matlab.mixin.CustomCompactDisplayProvider. To customize the
compact display for single-line and columnar layouts, place a call to the
widthConstrainedDataRepresentation utility method within the
compactRepresentationForColumn methods, respectively. Specify the
Annotation name-value argument so that MATLAB® displays an annotation for each row of the object array that includes
classdef WeekDays < matlab.mixin.CustomCompactDisplayProvider enumeration Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday end methods function rep = compactRepresentationForSingleLine(obj,displayConfiguration,width) [rep,~] = widthConstrainedDataRepresentation(obj,displayConfiguration,width, ... Annotation=annotation(obj)); end function rep = compactRepresentationForColumn(obj,displayConfiguration,width) [rep,~] = widthConstrainedDataRepresentation(obj,displayConfiguration,width, ... 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 in the Command Window. Additionally,
because the second row of the array includes the enumeration member
WeekDays.Saturday, 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. MATLAB displays as many leading array elements as possible and uses an ellipsis
symbol to represent the omitted elements.
T.FreeLunchDays = repmat(WeekDays.Friday,2,52)
T = 2×2 table Location FreeLunchDays __________ ______________________________________________ "Boston" Friday Friday Friday Friday … "New York" Friday Friday Friday Friday …