how to show custom properties in workspace

31 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

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
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.

Categories

Find more on Customize Object Display for Classes in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!