how to show custom properties in workspace
10 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
Rik
2023 年 5 月 12 日
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.
回答 (1 件)
Vinayak Gupta
2023 年 6 月 1 日
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:
参考
カテゴリ
Help Center および File Exchange で Subclass Definition についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!