Main Content

compactRepresentationForSingleLine

Class: matlab.mixin.CustomCompactDisplayProvider
Namespace: matlab.mixin

Customize single-line compact display representation of object array

Since R2021b

Description

example

rep = compactRepresentationForSingleLine(obj,displayConfiguration,width) returns the single-line compact display representation of the object array obj. To provide a suitable representation, the method uses the current display context displayConfiguration and the available character width provided by the container that must display obj.

compactRepresentationForSingleLine is called by containers that support a single-line display layout; for example, when your object is displayed within a structure or cell array, or as a property of another object. The default implementation of the method uses the array dimensions and class name to represent obj.

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.

Available character width to display the object array, specified as a positive double scalar.

Output Arguments

expand all

Single-line compact display representation of the object array to display, returned as a matlab.display.CompactDisplayRepresentation object.

Examples

expand all

To display custom information about the objects of your class when they are contained within a structure, cell array, or another object, derive your class from the matlab.mixin.CustomCompactDisplayProvider interface and override the compactRepresentationForSingleLine method.

In your current folder, create the Weekdays enumeration class by subclassing matlab.mixin.CustomCompactDisplayProvider. Customize the compact display for single-line layout by fitting as many elements of the object array within the available character width as possible, and also adding an annotation if the array includes weekend days. To customize the compact display, override the compactRepresentationForSingleLine method using a call to the widthConstrainedDataRepresentation utility method.

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 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 few Weekdays objects. MATLAB® displays all the array elements in a single line. Additionally, because the array includes the enumeration member WeekDays.Saturday, 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)

Now, create another 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    …    ]

Tips

  • The CustomCompactDisplayProvider interface provides utility methods that you can call from within compactRepresentationForSingleLine to conveniently customize the single-line compact display representation of your objects:

Version History

Introduced in R2021b