Main Content

matlab.display.PlainTextRepresentation Class

Namespace: matlab.display
Superclasses: matlab.display.CompactDisplayRepresentation

Compact display representation using data in object array

Since R2021b

Description

The matlab.display.PlainTextRepresentation class provides a compact display representation of an object array using plain text to show all or a portion of the data in the array.

Class Attributes

Sealed
true

For information on class attributes, see Class Attributes.

Creation

Description

rep = matlab.display.PlainTextRepresentation(obj,dataRepresentation,displayConfiguration) uses the current compact display configuration to construct a PlainTextRepresentation object with its Representation property set to dataRepresentation.

example

rep = matlab.display.PlainTextRepresentation(obj,dataRepresentation,displayConfiguration,Annotation=annotation) also sets the Annotation property to annotation.

Input Arguments

expand all

Object array to display, specified as an object array of a class derived from matlab.mixin.CustomCompactDisplayProvider.

Description of the current display context, specified as a matlab.display.DisplayConfiguration object.

Properties

expand all

Textual representation of the data in the object array, specified as an N-by-1 string array. The shape of Representation depends on the display layout:

  • Single-line layout — A padded string scalar

  • Columnar layout — An N-by-1 array of padded string scalars, where N is the number of rows in the object array

Attributes:

GetAccess
public
SetAccess
immutable

Descriptive comment about the object array shown as part of its compact display representation, specified as an N-by-1 string array. The shape of Annotation depends on the display layout:

  • Single-line layout — A string scalar

  • Columnar layout — An N-by-1 array of string scalars, where N is the number of rows in the object array

Attributes:

GetAccess
public
SetAccess
immutable

Finalized padded display text, specified as an N-by-1 string array. The shape of PaddedDisplayOutput depends on the display layout:

  • Single-line layout — A padded string scalar

  • Columnar layout — An N-by-1 array of padded string scalars, where N is the number of rows in the object array

Attributes:

GetAccess
public
SetAccess
immutable

Character width of the finalized padded display text, specified as a numeric array. The shape of CharacterWidth depends on the display layout:

  • Single-line layout — A numeric scalar

  • Columnar layout — An N-by-1 array of numeric scalars, where N is the number of rows in the object array

Attributes:

GetAccess
public
SetAccess
immutable

Examples

collapse all

Customize the way your objects are displayed in a structure by constructing the required CompactDisplayRepresentation objects.

In your current folder, create the Weekdays enumeration class by subclassing the matlab.mixin.CustomCompactDisplayProvider interface. Customize the compact display for single-line layout by fitting all elements of the object array within the available space, or else by using its dimensions and class name. To customize the compact display, construct either a PlainTextRepresentation object or a DimensionsAndClassNameRepresentation object within the compactRepresentationForSingleLine method, depending on the shape of the object array.

classdef WeekDays < matlab.mixin.CustomCompactDisplayProvider
    enumeration
        Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday
    end

    methods
        function rep = compactRepresentationForSingleLine(obj,displayConfiguration,~)
            import matlab.display.PlainTextRepresentation
            import matlab.display.DimensionsAndClassNameRepresentation
            if isrow(obj)
                % Fit all array elements in the available space, or else use
                % the array dimensions and class name
                str = string(obj);  % Call the string converter method on obj
                dataRepresentation = "";
                for i = 1:numel(str)  % Build the padded string
                    if i > 1
                        dataRepresentation = dataRepresentation + ...
                            displayConfiguration.InterElementDelimiter;
                    end
                    dataRepresentation = dataRepresentation + str(i);
                end
                % Represent the array using the padded string
                rep = PlainTextRepresentation(obj,dataRepresentation,displayConfiguration);
            else    % obj is not a row vector
                % Represent the array using its dimensions and class name
                rep = DimensionsAndClassNameRepresentation(obj,displayConfiguration);
            end
        end
    end
end

In the Command Window, create a structure with a field that contains a row vector of Weekdays objects. MATLAB® can display all the array elements in a single line.

s = struct("FreeLunchDays",[WeekDays.Monday WeekDays.Wednesday WeekDays.Friday])
s = 

  struct with fields:

    FreeLunchDays: [Monday    Wednesday    Friday]

Now, assign a 2-by-3 Weekdays array to s.FreeLunchDays. Because the new array is not a row vector, MATLAB displays only its dimensions and class name.

days = [WeekDays.Monday WeekDays.Wednesday WeekDays.Friday; ...
    WeekDays.Tuesday WeekDays.Thursday WeekDays.Saturday];
s.FreeLunchDays = days
s = 

  struct with fields:

    FreeLunchDays: [2×3 WeekDays]

Tips

  • Typically, you are not required to instantiate the PlainTextRepresentation class directly. The CustomCompactDisplayProvider interface provides utility methods that return a PlainTextRepresentation object tailored to your specific compact display requirements:

    • To construct a PlainTextRepresentation object from all data in an object array, use the fullDataRepresentation method.

    • To construct a PlainTextRepresentation object from a portion of data in an object array, use the partialDataRepresentation method.

    • To construct a PlainTextRepresentation object from as much of an object array's data as possible based on the available width, use the widthConstrainedDataRepresentation method.

Version History

Introduced in R2021b