Main Content

compactRepresentationForColumn

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

Customize columnar compact display representation of object array

Since R2021b

Description

rep = compactRepresentationForColumn(obj,displayConfiguration,width) returns the columnar 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.

compactRepresentationForColumn is called by containers that support a columnar display layout; for example, when your object is displayed within a MATLAB® table. The default implementation of the method uses the array dimensions and class name to represent obj.

example

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

Columnar 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 table, derive your class from the matlab.mixin.CustomCompactDisplayProvider interface and override the compactRepresentationForColumn method.

In your current folder, create the Weekdays enumeration class by subclassing matlab.mixin.CustomCompactDisplayProvider. Customize the compact display for columnar layout by fitting all elements of the object array within the available character width, if possible, and also adding an annotation for each row of the object array that includes weekend days. To customize the compact display, override the compactRepresentationForColumn method using a call to the fullDataRepresentation utility method.

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

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

Now, 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

Tips

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

Version History

Introduced in R2021b