how to show custom properties in workspace

7 vues (au cours des 30 derniers jours)
Thomas Michiels
Thomas Michiels le 11 Mai 2023
Commenté : Thomas Michiels le 6 Juin 2023
As we have the ability to define custom behavior for classes using subsref/subsassing or redefinesDpt/RedefinesParen/... we can create classes that act like they have more properties than they have.
using the dynamicprops subclass is super slow when adding new properties, where putting them on a structure is fast. there are many options to overwrite the Display in the console. But i have yet to find a way to add these custom behavior properties to the workspace.
How would you go about customising the properties shown in the workspace/variable window? i have tried to overwrite the properties method, tried to inherit from customDisplay and many other thing.
Thanks
view i want to customise:
code of redefinesDot:
classdef DynPropHelper < matlab.miin.indexing.RedefinesDot & handle
properties(Access=private)
AddedFields struct = struct;
end
methods(Access=protected) % redefinesDot mixin
function varargout = dotReference(obj,indexOp)
if isfield(obj.AddedFields, indexOp(1).Name)
temp = obj.AddedFields.(indexOp(1));
else
throw(MException('MATLAB:noSuchMethodOrField','No property or method called %s exists on this object', indexOp(1).Name));
end
if ~isscalar(indexOp)
[varargout{1:nargout}] = temp.(indexOp(2:end));
else
[varargout{1:nargout}] = temp;
end
end
function obj = dotAssign(obj,indexOp,varargin)
if ~isfield(obj.AddedFields, indexOp(1).Name)
% this allows creating chain tags + usually we call
% function here to do checks / meta data gathering etc...
obj.AddedFields.(indexOp(1).Name) = [];
end
if isscalar(indexOp)
obj.AddedFields.(indexOp(1).Name) = varargin{1};
else
%I know, this should do the same checks as DotReference,
%I'm simplifying things here
obj.AddedFields.(indexOp) = varargin{:};
end
end
function n = dotListLength(obj,indexOp,indexContext)
n = listLength(obj.AddedFields,indexOp,indexContext);
end
end
end
  2 commentaires
Rik
Rik le 12 Mai 2023
I suspect someone who can help you will want to know how exactly you add properties to your object.
Also, please put the image here instead of an external service.
Thomas Michiels
Thomas Michiels le 12 Mai 2023
will update my question later today

Connectez-vous pour commenter.

Réponses (1)

Vinayak Gupta
Vinayak Gupta le 1 Juin 2023
Hey Thomas
As the properties created are private, it won’t be possible to display them is custom properties, but we can display them as text list via custom display.
% Inherit the class from 3 superclasses
classdef DynPropHelper < matlab.mixin.indexing.RedefinesDot & handle & matlab.mixin.CustomDisplay
Later add a protected method for custom display.
methods (Access=protected)
function displayScalarObject(obj)
disp(obj.AddedFields);
end
end
This should display in the variables view as
If you want added functionality of being available as field instead of text. You can consider making the field as (SetAccess=private, GetAccess=public)
Refer to the following to learn more on CustomDisplay:
  2 commentaires
Thomas Michiels
Thomas Michiels le 6 Juin 2023
> As the properties created are private, it won’t be possible to display them is custom properties, but we can display them as text list via custom display.
this is actually part of my issue, by allowing to redefine the dot operation, more properties are publicly available than in the public properties list. The whole point of RedefinesDot is that it is only triggered for unknown properties.
That is why it would make sense to also have the ability to customise what is shown in the workspace/variable explorer. If you can customise the console output, the way you interact with your object and everything that influences the experience of the person interacting with instances of the class. It is very jarring that the workspace is not customisable.
Thomas Michiels
Thomas Michiels le 6 Juin 2023
and if it is meant to just show the state of the object to aid the developper, then it would be more logical to also show private/protected properties. The fact that it only shows public properties shows that is meant for the user of the code and not the core developer, thus it should be customisable. Now it is in a limbo inbetween both target groups

Connectez-vous pour commenter.

Catégories

En savoir plus sur Subclass Definition dans Help Center et File Exchange

Produits


Version

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by