How to display a full table when it's a class property
11 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Analyze Simulation Results 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!