If I have a value class like this:
classdef MyVal
properties
foo
end
methods
function self = MyVal(varargin)
if nargin > 0
self.foo = varargin{1};
end
end
end
end
and use it in a table:
foo(1) = MyVal(1); foo(2) = MyVal(2);
t = table(foo')
the output is:
t =
2×1 table
Var1
___________
[1×1 MyVal]
[1×1 MyVal]
Is there any method which has to be defined in MyVal (or any property of the table) which allows to change the representation of the value in the table? I do not want to convert the data passed to table since I'd like to retrieve instances of MyVal when I index in the table.
You could create a custom wrapper for
table. This is slightly pokey because MATLAB doesn't let you inherit from thetableclass, but by overloading thesubsrefandsubsasgnoperators you can get most of the functionality anyway...I've included the class at the bottom of this answer since it's a bit long, usage would look like this:
You can also use this to extract the numeric data (i.e. the
fooproperty alongside other data) by usingt.numeric, although I suspect you will need to usetable2arrayon this for use withuitable, as you would with a normaltable.