how to show custom properties in workspace

14 views (last 30 days)
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 Comments
Rik
Rik on 12 May 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.

Sign in to comment.

Answers (1)

Vinayak Gupta
Vinayak Gupta on 1 Jun 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 Comments
Thomas Michiels
Thomas Michiels on 6 Jun 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 on 6 Jun 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

Sign in to comment.

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!