Customize how my class is displayed as a property of any other class.

3 views (last 30 days)
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??
Siddharth Bhutiya
Siddharth Bhutiya on 19 Mar 2020
As Guillaume mentioned, there have been some perfromance improvements to datetime/durations, specifically in subscripted assignment and text parsing in 2019b and 2020a. You can read about it here.
@Nikolaus If you could share some samples of performance bottleneck use cases that you mentioned, I can see if there are any improvements that can be made, so that you can avoid rewriting your exisinting code.

Sign in to comment.

Accepted Answer

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.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!