Customize how my class is displayed as a property of any other class.
6 views (last 30 days)
Nikolaus Koopmann on 8 Mar 2020
I wrote a custom datetime class, because built-in datetime is often too slow.
My class inherits from matlab.mixin.CustomDisplay so that I can modify how it displays in the terminal. In particular I want to modify how it shows up within the displaying of some (any) other class to which it is a property.
I.e. let there be a class x.m with a property
dtime (1,1) my.datetime = my.datetime(now)
Now, if I display an instance of x.m in the terminal, it will show something like:
x with properties:
dtime: [1x1 my.datetime]
What I want it to look like is
x with properties:
dtime: 08.03.2020 17:39
where that datestring corresponds to a (dependent) property of +my/datetime.m.
The built-in datetime does something like that! How I can achieve this??
Jennifer Black on 22 Apr 2022
As of R2022a, MATLAB provides a mixin class CustomCompactDisplayProvider for customizing the way instances of your class will display when held as values within some other container, such as a struct, cell array, or other object.
To support a custom contained display format for the custom datetime example, add this code to your class:
classdef mydatetime < matlab.mixin.CustomCompactDisplayProvider
function rep = compactRepresentationForSingleLine(obj, configuration, width)
When an instance of your class is being displayed within a cell of a cell array, a field of a struct, or a property of some other object, MATLAB will invoke the compactRepresentationForSingleLine method. In your implementation, return a string representation of how you want your object to be displayed. Make sure that your string is no longer than the specified width input value.
Please see our documentation page for more details and an example of how to make use of the new interface.
More Answers (1)
Guillaume on 9 Mar 2020
"I can make my class inherit from your mentioned parent classes, but i cannot view those classes.. so there is no way of finding out which method to overwrite i guess?"
First, I've never tried to override the display of classes as you want to do, so disclaimer: I've no idea if matlab.mixin.internal.MatrixDisplay allows you to do that or not.
Yes, there is a way of finding out all the methods of the class as well as their signature, attributes and names of inputs and outputs. There is however no way to see their source code so you still have to do a fair amount of reverse engineering.
mc = meta.class.fromName('matlab.mixin.internal.MatrixDisplay') %obtain reflection information of the class
mc.MethodList is an array of meta.method objects describing all the methods of the class (included private/protected/hidden methods). You can see that there is one abstract method disp (public, hidden) which takes two inputs helpfully named rhs1 and rhs2. You probably also need to override displayImpl (protected, hidden) which also takes two inputs.
Another disclaimer: The above is valid in R2019b. None of these classes are documented so they can change/disappear in the next version. If you have access to the R2020a pre-release, I'd strongly recommend having a look at the parent classes of datetime. Unfortunately, we're not allowed to discuss the pre-release.