How to display a full table when it's a class property

11 ビュー (過去 30 日間)
Arwel
Arwel 2021 年 2 月 26 日
回答済み: Aakash Mehta 2021 年 3 月 31 日
Hi,
Suppose I have a class which has a table as a parameter, like this...
classdef exampleClass1 < handle
properties
T = table;
end
methods
% Constructor
function obj = exampleClass1()
sz = [3 3];
varTypes = {'double','double','double'};
varNames = {'Min','Value','Max'};
obj.T = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
obj.T(1,:) = {1 2 3 };
end
end
end
Then this class is a subclass of a second one, like so....
classdef exampleClass < exampleClass1
properties
myParameter1
myParameter2
end
end
When I make an instance of exampleClass, it gets displayed like this....
>> myInstance = exampleClass
myInstance =
exampleClass with properties:
myParameter1: []
myParameter2: []
T: [3×3 table]
But actually this is not how I want the class to display. Actually I would like it to show the full expanded view of the table as default e.g.
>> myInstance.T
ans =
3×3 table
Min Value Max
___ _____ ___
1 2 3
0 0 0
0 0 0
How can I make it so that the expanded version of the table is displayed by default (along with the other properties) when i display 'exampleClass'. I know this is probably something to do with 'matlab.mixin.CustomDisplay', but I can't quite figure out what.
Cheers,
Arwel

回答 (1 件)

Aakash Mehta
Aakash Mehta 2021 年 3 月 31 日
By overloading the disp method for your class you can customize how properties are displayed.
In exampleClass1, Use the matlab.mixin.SetGet class to derive classes that inherit a set and get method interface.
classdef exampleClass1 < matlab.mixin.SetGet
properties
T = table;
end
methods
% Constructor
function obj = exampleClass1()
sz = [3 3];
varTypes = {'double','double','double'};
varNames = {'Min','Value','Max'};
obj.T = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
obj.T(1,:) = {1 2 3 };
end
end
end
Overload the disp method in exampleClass.
classdef exampleClass < exampleClass1
properties
myParameter1
myParameter2
end
methods
function disp(obj)
p=properties(obj);
for i=1:length(p)
propval=get(obj,p);
disp(p{i})
disp(propval{1,i})
end
end
end
end
Now when you make an instance of exampleClass, it gets displayed like this
>> myInstance = exampleClass
myInstance =
myParameter1
myParameter2
T
Min Value Max
___ _____ ___
1 2 3
0 0 0
0 0 0
Change the disp method as per your expected output.
For more details of overloading the disp method, refer to the following link.

カテゴリ

Help Center および File ExchangeAnalyze Simulation Results についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by