MATLAB: Is there an example on overloading the SUBSREF function for the class, by using built-in SUBSREF

MATLABoopsoverloadoverloadingsubsref

I would like to create a comma separated list output from SUBSREF.

Best Answer

You will be able to return a comma-separated list from your class by using the builtin SUBSREF function to overload your object's SUBSREF function.
For example, if you have a class @testobj, then you could implement SUBSREF as follows:
function varargout = subsref(obj, S)
[varargout{1:nargout}] = builtin('subsref',obj,S);
This should just use the built-in structure SUBSREF to access the data. You may want to add special cases, for example if you're indexing your object such as the following:
x(1:2)
Then you need to make sure you convert the varargout elements to class objects, such as the following:
function varargout = subsref(obj, S)
[varargout{1:nargout}] = builtin('subsref',obj,S);
if numel(S) == 1 & S.type=='()'
for i = 1: nargout,
varargout{i} = class(varargout{i},'testobj');
end
If you are experiencing problems with SUBSREF not returning the correct number of comma separated list outputs, see Related Solution "When indexing into my MATLAB object and returning a comma separated list, why don't I get the correct number of outputs?" listed below.