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

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

 Réponse acceptée

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.

1 commentaire

This doesn't work if nargout is zero.
For a MATLAB structure, a, a.b returns a CSL if length(a>1) ... why can't I do that?
Is there any other way to return a CSL?
I have the thing I want to return in a cell array (xx) and nargout is zero ... I have tried many different ways and none works:
1. varargout = xx;
2. SCool = substruct('{}',{':'}); [varargout{1:length(xx)}] = builtin('subsref',xx,SCool); (suggestion from above)
3. [varargout{1:length(xx)}] = deal(xx{:});
In each and every case, varargout is a cell array with more than one element but every time the answer returns only the first element of the cell array ... no CSL. If nargout is 0 and I return varargout with more than one element, it should return a CSL which I can subsequently pass into horzcat, deal, or another function that accepts a CSL. If anyone can Help, I'd appreciate it.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by