Main Content

matlab.mixin.CustomCompactDisplayProvider Class

Namespace: matlab.mixin

Interface for customizing object display within containers

Since R2021b

Description

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 matlab.mixin.CustomCompactDisplayProvider.

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.

Class Attributes

Abstract
true
HandleCompatible
true

For information on class attributes, see Class Attributes.

Methods

expand all

Examples

collapse all

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 subclassing matlab.mixin.CustomCompactDisplayProvider. To customize the compact display for single-line and columnar layouts, override the compactRepresentationForSingleLine and 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 widthConstrainedDataRepresentation utility method within compactRepresentationForSingleLine.

  • 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 fullDataRepresentation utility method within compactRepresentationForColumn.

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 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)

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    …    ]

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 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) 

Update the 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 History

Introduced in R2021b

expand all