Effacer les filtres
Effacer les filtres

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

23 vues (au cours des 30 derniers jours)
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??
  7 commentaires
Nikolaus Koopmann
Nikolaus Koopmann le 19 Mar 2020
oh really?! nice! this comes just in time.. i was about to rewrite A LOT of code back to datenums..
Siddharth Bhutiya
Siddharth Bhutiya le 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.

Connectez-vous pour commenter.

Réponse acceptée

Jennifer Black
Jennifer Black le 22 Avr 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.

Plus de réponses (1)

Guillaume le 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.
  3 commentaires
Guillaume le 10 Mar 2020
Indeed, after some testing it doesn't appear that this kind of display is implemented by matlab.mixin.internal.MatrixDisplay. In fact, I'm not sure what kind of display it implements.
I've modified your original X to:
classdef X < handle & matlab.mixin.CustomDisplay
dt datetime
qdt qdatetime
qdts qdatetime
function obj = X()
obj.dt = datetime(2000,1,1);
obj.qdt = qdatetime(datenum(obj.dt));
obj.qdts = [obj.qdt obj.qdt];
methods (Access = protected)
function displayScalarObject(this)
and by setting a break point in the displayScalarObject method, you can then step through what is happening during the display. In particular, you can see which datetime methods are called. There's only two methods that are called, the datetime.size method, which is normal it also happens with qdatetime and the datetime.cellstr method which is unexpected. Unfortunately, even if you implement a qdatetime.cellstr it doesn't get called automatically. This leads me to believe that the display of datetime within objects is actually hardcoded in matlab, so I'm afraid that it doesn't look like there's any way to implement the same.
Nikolaus Koopmann
Nikolaus Koopmann le 11 Mar 2020
:( ok.. thanks for your help Guillaume!!

Connectez-vous pour commenter.


En savoir plus sur Data Type Identification dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by