Main Content

Custom Compact Display Interface

In addition to the custom display options described in Custom Display Interface, you can also customize the way MATLAB® displays objects in compact display scenarios. Compact display scenarios occur when an object array is inside a container, such as a structure, cell array, or table. You can customize the way object arrays are displayed in a compact display by inheriting from matlab.mixin.CustomCompactDisplayProvider.

(Since R022b) The rules implemented by CustomCompactDisplayProvider are honored by the Live Editor, the Variables editor in MATLAB Online™, and the single-line 'Value' field in the Workspace panel in MATLAB Online.

Customization Options Available for Compact Display

CustomCompactDisplayProvider provides customization options in two main areas of compact displays:

  • Single-line layout — The display of the object array is confined to a single line. This display layout occurs when the object array is contained within a structure, cell array, or property of a MATLAB object. Override the CustomCompactDisplayProvider method compactRepresentationForSingleLine to customize this layout.

  • Columnar layout — The object array is displayed in multiple rows as part of column-oriented or tabular data. This display layout occurs when the object array is contained within a table variable. Override the CustomCompactDisplayProvider method compactRepresentationForColumn to customize this layout.

Three additional CustomCompactDisplayProvider utility methods enable you to control how much of the data is used to construct a display of the object array:

  • partialDataRepresentation — The compact display of the object array is constructed from only the leading elements of the data set or only the leading and trailing elements, omitting the values in between.

  • fullDataRepresentation — The compact display of the object array is constructed based on the entire set of data.

  • widthConstrainedDataRepresentation — The compact display of the object array is constructed from the data based on a width constraint you provide.

Each of these three utility methods also provides an Annotation name-value argument, which enables you to include a row vector of strings to serve as descriptors in your compact displays. For example, in the case of a class representing polynomials, adding an annotation can be useful when only the dimensions and class of the data fit in the display:

struct with fields:

   prop1: [1x3 sym] (Third-order polynomial)

Designing a Class with a Customized Compact Display

When considering how to customize the compact display for a class, first write the class without any customization and test to see if the default display options need to be changed for your application. For example, this class stores a single polynomial as a string and its order as a double.

classdef Polynomial
    properties
        polynomialString string {mustBeScalarOrEmpty};
        polynomialOrder double {mustBeScalarOrEmpty};
    end
    methods
        function obj = Polynomial(polyString,polyOrder)
            obj.polynomialString = polyString;
            obj.polynomialOrder = polyOrder;
        end
    end
end

The full code of this class with customized compact display functionality is available at the end of the example.

Create an instance of Polynomial. Display the instance in a cell array to check the single-line layout.

poly = "(x^10)/100 + 300*x^9 + 200*x^8 + 4000*x^7 + 600*x^6 + 700*x^5 + 45*x^4 + 90*x^3 + 4*x^2 + 100*x^(1/2) + 1";
a = Polynomial(poly,10);
{a}
ans =

  1×1 cell array

    {1×1 Polynomial}

The single-line layout does not provide users with much information. Change Polynomial to inherit from matlab.mixin.CustomCompactDisplayProvider and override the compactRepresentationForSingleLine method. Call the utility method widthConstrainedDataRepresentation with AllowTruncatedDisplayForScalar set to true. MATLAB now truncates the polynomial string based on the width available.

function displayRep = compactRepresentationForSingleLine(obj,displayConfiguration,width)
   displayRep = widthConstrainedDataRepresentation(obj,displayConfiguration,width,...
      StringArray=obj.polynomialString,AllowTruncatedDisplayForScalar=true);
end

Now, the display of the instance in a cell array contains as much of the string as possible.

{a}
ans =

  1×1 cell array

    {[(x^10)/100 + 300*x^9 + 200*x^8 + 4000*x^7 + 600*x^6 + 700*x^5 + 45*x^4 + 90*x^3 + 4*x^2 + 100*x^(1/2) …]}

For a table display, the default shows only the class and dimensions, so override compactRepresentationForColumn. The method returns an instance of matlab.display.DimensionsAndClassNameRepresentation with an annotation, which in this case identifies the order of the polynomial. To cover the case when a table has a row vector of Polynomial instances in a single entry, the method reshapes the polynomialOrder property to match the shape of the instance and then finds the maximum order in that row for display.

function displayRep = compactRepresentationForColumn(obj,displayConfig,width) 
   import matlab.display.DimensionsAndClassNameRepresentation;
   polOrder = reshape([obj.polynomialOrder],size(obj));
   maxPolOrderPerRow = max(polOrder,[],2); 
   annotationStr = "Polynomial of order " + string(maxPolOrderPerRow);
   displayRep = DimensionsAndClassNameRepresentation(obj,...
      displayConfig,Annotation=annotationStr);
end

With this code in place, you can create a table of estimation results, for example, that identifies the order of the polynomial. Create a second Polynomial, b. Then create two tables, one with a and b in a column, and one with a and b in the same row.

b = Polynomial("x^3 + x^2 + x",3);
Result = [1;2];
Estimate = [a;b];
T = table(Result,Estimate)
T =

  2×2 table

    Result                    Estimate                 
    ______    _________________________________________

      1       1×1 Polynomial  (Polynomial of order 10)
      2       1×1 Polynomial  (Polynomial of order 3) 

With a and b in the same row, only the maximum order is displayed.

Result = 1;
Estimate = [a b];
T1 = table(Result,Estimate)
T1 =

  1×2 table

    Result                    Estimate                 
    _______    _________________________________________

       1       1×2 Polynomial  (Polynomial of order 10) 

 Complete Polynomial Class

Related Topics