Can the "Value" display of variables be customized?

6 ビュー (過去 30 日間)
Frederick Zittrell
Frederick Zittrell 2019 年 10 月 16 日
編集済み: Justin Rehak 2024 年 1 月 17 日
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?
  1 件のコメント
Captain Karnage
Captain Karnage 2023 年 5 月 5 日
編集済み: Captain Karnage 2023 年 5 月 5 日
I dug a little further, and it looks like it is possible, using Java. When I have time, I will see if I can code something up myself, but I wanted to share this link as soon as I could, as it will probably take me a while to actually generate working code. Java is the language I've used the least so I'd have to brush up on my Java skills again.
Two caveats with this link:
  1. They are customizing the "Bytes" column - but I assume it would be a trivial change to make it the "Value" column.
  2. The code in the article changes the column display for every variable. Some non-trivial code would need to be added to seek out variables of one of your custom classes and change the display specific to that class.
Or, alternately to #2, you could have it seek ANY custom class, seek out a certain property name and, if it exists, display that property name (e.g. the "id" property in your case).
If it were me, I would actually make a hidden, dependent property called "Value" or something generic like that for it to seek out, then I would add a getter for that property in each custom class and set it equal to the value I want to display in the Workspace pane. Then code the Java to display the "Value" property in the "Value" column for all custom classes that have a "Value" property.
Maybe there is a way with code in the Class's .m file, but if there is - like you, I haven't found it yet nor I have I found evidence it's not possible at all.

サインインしてコメントする。

回答 (2 件)

Steven Lord
Steven Lord 2019 年 10 月 16 日
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.
  3 件のコメント
Fabian Müller
Fabian Müller 2021 年 1 月 7 日
Any updates on this? Did you figure out if it's possible to customize this "value" display? Would help me also a lot. Thanks
Frederick Zittrell
Frederick Zittrell 2021 年 1 月 7 日
No, unfortunately not -- I did not find a solution and I did not find a clear answer that it is definitely not possible.

サインインしてコメントする。


Justin Rehak
Justin Rehak 2024 年 1 月 17 日
編集済み: Justin Rehak 2024 年 1 月 17 日
Can you make your class inherit from double? Here's an example of something I made recently. It appears as a double in the workspace, but I can override the display methods to show metric suffixes in the command window.
classdef capacitance < double & matlab.mixin.CustomDisplay
properties (Hidden)
str
end
methods
function obj = capacitance(value)
obj = obj@double(value)
obj.str = obj.getSuffix();
end
function str = getSuffix(obj)
if obj > 1e-3
str = obj*1e3 + "m";
elseif obj > 1e-6
str = obj*1e6 + "µ";
% and so on %
end
end
end
%% Custom display methods
methods (Access = protected)
% Custom display here %
end
end
The following documentation was helpful when I ran into issues with arrays of this class. Subclasses of Built-In Types with Properties

カテゴリ

Help Center および File ExchangeGraphics Object Identification についてさらに検索

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by