table 中值 class 的表示
Representation of value class in table
如果我有这样的值 class:
classdef MyVal
properties
foo
end
methods
function self = MyVal(varargin)
if nargin > 0
self.foo = varargin{1};
end
end
end
end
并在 table:
中使用它
foo(1) = MyVal(1); foo(2) = MyVal(2);
t = table(foo')
输出为:
t =
2×1 table
Var1
___________
[1×1 MyVal]
[1×1 MyVal]
是否有任何方法必须在 MyVal
(或 table 的任何 属性)中定义,以允许更改 [=27] 中值的表示=]?我不想转换传递给 table
的数据,因为我想在索引 table.
时检索 MyVal
的实例
只有一半答案
我找不到自定义显示对象 table
的方法,但是如果您可以使用对象的 arrays
来实现,那么有一种方法,在 Customize Object Display for Classes.
对于您的示例,您必须使用 matlab.mixin.CustomDisplay
派生 class,然后覆盖 displayNonScalarObject
方法:
classdef MyVal < matlab.mixin.CustomDisplay
properties
foo
end
methods
function self = MyVal(varargin)
if nargin > 0
self.foo = varargin{1};
end
end
end
methods (Access = protected)
function displayNonScalarObject(objAry)
dimStr = matlab.mixin.CustomDisplay.convertDimensionsToString(objAry);
cName = matlab.mixin.CustomDisplay.getClassNameForHeader(objAry);
headerStr = [dimStr,' ',cName,' members:'];
header = sprintf('%s\n',headerStr);
disp(header)
for ix = 1:length(objAry)
o = objAry(ix);
% OPTION 1:
% For a class with a sinle property, no need to call the
% big guns, just build your own display:
disp( [num2str(ix) ': foo = ' num2str(o.foo) ] ) ;
% OR OPTION 2: (comment option1 if you use this)
% If your class had multiple properties and you want to
% display several of them, use the MATLAB built-in functions:
% create a structure with the property names you need
% displayed:
% propList = struct('foo',o.foo,'prop2',o.prop2,'prop3',o.prop3);
% Then let MATLAB handle the display
% propgrp = matlab.mixin.util.PropertyGroup(propList);
% matlab.mixin.CustomDisplay.displayPropertyGroups(o,propgrp);
end
end
end
end
现在,如果您构建 class 的数组,显示将如下所示:
>> foo(1) = MyVal(1); foo(2) = MyVal(2);
>> foo
foo =
1x2 MyVal members:
1: foo = 1
2: foo = 2
当然这只是一个例子,是高度可定制的。不幸的是,我找不到使 table
对象工作的方法。
您可以为 table
创建自定义包装器。这有点古怪,因为 MATLAB 不允许您从 table
class 继承,但通过重载 subsref
和 subsasgn
运算符,您仍然可以获得大部分功能。 ..
我在这个答案的底部包含了 class,因为它有点长,用法如下所示:
>> foo(1) = MyVal(1); foo(2) = MyVal(2);
>> t = MyTable(foo') % Omitting the ";" calls our overloaded "disp" function
t =
Var1
____
1
2
您还可以通过使用 t.numeric
使用它来提取数字数据(即 foo
属性 和其他数据),尽管我怀疑您需要使用 table2array
与 uitable
一起使用,就像与普通 table
.
一起使用一样
classdef MyTable
properties ( Access = private )
table_
end
properties ( Dependent = true )
numeric
end
methods % Constructor and getter
function obj = MyTable( varargin )
% Store as a normal table internally
obj.table_ = table( varargin{:} );
end
function num = get.numeric( obj )
% By calling obj.numeric, output the table but with MyVal
% objects replaced by their "foo" property.
% This could be passed into a uitable, and is used in disp()
cls = varfun( @(x)isa(x,'MyVal'), obj.table_, 'OutputFormat', 'uniform' );
num = obj.table_;
for ii = find(cls)
num.(num.Properties.VariableNames{ii}) = [num.(num.Properties.VariableNames{ii}).foo].';
end
end
end
methods % Overloaded to emulate table behaviour
function disp( obj )
% Overload the disp function (also called when semi colon is
% omitted) to output the numeric version of the table
disp( obj.numeric );
end
% Overload subsref and subsasgn for table indexing
function varargout = subsref( obj, s )
[varargout{1:nargout}] = builtin( 'subsref', obj.table_, s );
end
function obj = subsasgn( obj, s, varargin )
obj.table_ = builtin( 'subsasgn', obj.table_, s, varargin{:} );
end
% Have to overload size and isa for workspace preview
function sz = size( obj, varargin )
sz = size( obj.table_, varargin{:} );
end
function b = isa( obj, varargin )
% This is only OK to do because we overloaded subsref/subsasgn
b = isa( obj.table_, varargin{:} );
end
end
end
如果我有这样的值 class:
classdef MyVal
properties
foo
end
methods
function self = MyVal(varargin)
if nargin > 0
self.foo = varargin{1};
end
end
end
end
并在 table:
中使用它foo(1) = MyVal(1); foo(2) = MyVal(2);
t = table(foo')
输出为:
t =
2×1 table
Var1
___________
[1×1 MyVal]
[1×1 MyVal]
是否有任何方法必须在 MyVal
(或 table 的任何 属性)中定义,以允许更改 [=27] 中值的表示=]?我不想转换传递给 table
的数据,因为我想在索引 table.
MyVal
的实例
只有一半答案
我找不到自定义显示对象 table
的方法,但是如果您可以使用对象的 arrays
来实现,那么有一种方法,在 Customize Object Display for Classes.
对于您的示例,您必须使用 matlab.mixin.CustomDisplay
派生 class,然后覆盖 displayNonScalarObject
方法:
classdef MyVal < matlab.mixin.CustomDisplay
properties
foo
end
methods
function self = MyVal(varargin)
if nargin > 0
self.foo = varargin{1};
end
end
end
methods (Access = protected)
function displayNonScalarObject(objAry)
dimStr = matlab.mixin.CustomDisplay.convertDimensionsToString(objAry);
cName = matlab.mixin.CustomDisplay.getClassNameForHeader(objAry);
headerStr = [dimStr,' ',cName,' members:'];
header = sprintf('%s\n',headerStr);
disp(header)
for ix = 1:length(objAry)
o = objAry(ix);
% OPTION 1:
% For a class with a sinle property, no need to call the
% big guns, just build your own display:
disp( [num2str(ix) ': foo = ' num2str(o.foo) ] ) ;
% OR OPTION 2: (comment option1 if you use this)
% If your class had multiple properties and you want to
% display several of them, use the MATLAB built-in functions:
% create a structure with the property names you need
% displayed:
% propList = struct('foo',o.foo,'prop2',o.prop2,'prop3',o.prop3);
% Then let MATLAB handle the display
% propgrp = matlab.mixin.util.PropertyGroup(propList);
% matlab.mixin.CustomDisplay.displayPropertyGroups(o,propgrp);
end
end
end
end
现在,如果您构建 class 的数组,显示将如下所示:
>> foo(1) = MyVal(1); foo(2) = MyVal(2);
>> foo
foo =
1x2 MyVal members:
1: foo = 1
2: foo = 2
当然这只是一个例子,是高度可定制的。不幸的是,我找不到使 table
对象工作的方法。
您可以为 table
创建自定义包装器。这有点古怪,因为 MATLAB 不允许您从 table
class 继承,但通过重载 subsref
和 subsasgn
运算符,您仍然可以获得大部分功能。 ..
我在这个答案的底部包含了 class,因为它有点长,用法如下所示:
>> foo(1) = MyVal(1); foo(2) = MyVal(2);
>> t = MyTable(foo') % Omitting the ";" calls our overloaded "disp" function
t =
Var1
____
1
2
您还可以通过使用 t.numeric
使用它来提取数字数据(即 foo
属性 和其他数据),尽管我怀疑您需要使用 table2array
与 uitable
一起使用,就像与普通 table
.
classdef MyTable
properties ( Access = private )
table_
end
properties ( Dependent = true )
numeric
end
methods % Constructor and getter
function obj = MyTable( varargin )
% Store as a normal table internally
obj.table_ = table( varargin{:} );
end
function num = get.numeric( obj )
% By calling obj.numeric, output the table but with MyVal
% objects replaced by their "foo" property.
% This could be passed into a uitable, and is used in disp()
cls = varfun( @(x)isa(x,'MyVal'), obj.table_, 'OutputFormat', 'uniform' );
num = obj.table_;
for ii = find(cls)
num.(num.Properties.VariableNames{ii}) = [num.(num.Properties.VariableNames{ii}).foo].';
end
end
end
methods % Overloaded to emulate table behaviour
function disp( obj )
% Overload the disp function (also called when semi colon is
% omitted) to output the numeric version of the table
disp( obj.numeric );
end
% Overload subsref and subsasgn for table indexing
function varargout = subsref( obj, s )
[varargout{1:nargout}] = builtin( 'subsref', obj.table_, s );
end
function obj = subsasgn( obj, s, varargin )
obj.table_ = builtin( 'subsasgn', obj.table_, s, varargin{:} );
end
% Have to overload size and isa for workspace preview
function sz = size( obj, varargin )
sz = size( obj.table_, varargin{:} );
end
function b = isa( obj, varargin )
% This is only OK to do because we overloaded subsref/subsasgn
b = isa( obj.table_, varargin{:} );
end
end
end