MATLAB Answers

0

Can the "Value" display of variables be customized?

Frederick Zittrell さんによって質問されました 2019 年 10 月 16 日 14:20
最新アクティビティ Frederick Zittrell さんによって コメントされました 2019 年 10 月 17 日 8:44
Consider this simple class that contains data and a data set ID:
classdef myClass
properties
id
data
end
methods
function obj = myClass(id)
obj.id = id;
end
end
end
When a class instance is created with obj = myClass(1), MATLAB's workspace window displays the "Value" of the resulting variable as "1x1 myClass". Likewise, a vector of objects obj = [myClass(1), myClass(2)] is displayed in MATLAB's variable viewer as "1x1 myClass" in each table cell.
I would like MATLAB to display the value of each object's id property instead of "1x1 myClass". Is this possible?
I read about custom display, but all this seems to only affect methods like disp and details. Or am I missing something?

  0 件のコメント

サインイン to comment.

製品


リリース

R2019b

1 件の回答

Steven Lord
回答者: Steven Lord
2019 年 10 月 16 日 16:53

You are missing something. The methods that your class inherits when you subclass from matlab.mixin.CustomDisplay are used when displaying the object even if you don't explicitly call disp or display.
classdef myClass < matlab.mixin.CustomDisplay
properties
id
data
end
methods
function obj = myClass(id)
obj.id = id;
end
end
methods(Access=protected)
function displayScalarObject(obj)
if isnumeric(obj.id) || ischar(obj.id)
S = string(obj.id);
else
S = obj.id;
end
fprintf('id: %s\n', S);
end
end
end
Try constructing this using a number or text data as the ID.
>> Q = myClass(42)
Q =
id: 42
>> Q = myClass('abracadabra')
Q =
id: abracadabra
>> Q = myClass("MATLAB")
Q =
id: MATLAB
You may need or want to implement more of the methods inherited from matlab.mixin.CustomDisplay than just displayScalarObject. See this page for a more detailed description of when and how each of those methods are used when displaying your object.

  1 件のコメント

Frederick Zittrell 2019 年 10 月 17 日 8:44
Thank you for pointing this out, but I was already aware of this customization and the information in the link you provided. While this customization affects the variable display in the Command Window, it does not affect the "Value" display in the Workspace Window or when a multi-element array of objects is inspected (by double clicking the variable name in the Workspace Window). Consider this object array:
>> X = [myClass(1);myClass(2)]
X =
2×1 myClass array with properties:
id
data
In the Workspace Window, X has the "Value" 2x1 myClass, and the variable inspector shows this:
X.PNG
I would like to change the display behavior in this instance (and ideally also in the Workspace Window), not in the Command Window.
An example where MATLAB does something similar to what I would like to achieve is the display of cell arrays in the variable inspector. Consider these variables:
>> c1 = {'a'}
c1 =
1×1 cell array
{'a'}
>> c2 = {'a','b'}
c2 =
1×2 cell array
{'a'} {'b'}
>> c3 = {c1;c2}
c3 =
2×1 cell array
{1×1 cell}
{1×2 cell}
When inspecting c3, the variable inspector shows this:
c3.PNG
I could not find a hint in the documentation on how the display behavior in the Workspace Window and in the variable inspector can be customized.

サインイン to comment.



Translated by