nargout in subsref - Too many output arguments
3 views (last 30 days)
Show older comments
I am implementing a class and its subsref method. The class is essentially a 2D data structure. If a is an object of the class, for a{:,1:3}, I would like to get a subset of all rows and the first three columns.
However, in subsref, I found the nargout is 3 for a{:,1:3} but I have only one subset to return. Matlab then complains "Too many output arguments." or "Output argument "varargout{2}" (and maybe others) not assigned".
How to get around this? BTW, I am using Matlab R2009b.
Thank you for the help.
P.S.
function [varargout] = subsref(a,s)
%SUBSREF Subscripted reference for a SesameO object.
% B = SUBSREF(A,S) is called for the syntax A(I,J), A{I,J}, or A.VAR
% B = A(I,J) getSub
%
% B = A{I,J} getSubByIdx
%
switch s(1).type
case {'()', '{}'}
% common error checking
% indexing can only return a single thing.
% if nargout > 1
% error('SesameO:subsref:TooManyOutputs', ...
% 'Too many outputs.');
%
% No cascaded subscripts are allowed to follow parenthesis indexing.
if ~isscalar(s)
error('SesameO:subsref:InvalidSubscriptExpr', ...
'No cascaded subscripts are allowed.');
elseif numel(s(1).subs) > 2
error('SesameO:subsref:NDSubscript', ...
'SesameO array subscripts must be two-dimensional.');
end
end
switch s(1).type
case '()'
for i=1:length(s.subs)
if strcmp(s.subs{i}, ':')
s.subs{i} = [];
end
end
b=a.getSub(s.subs{:});
varargout{1} = b;
case '{}'
for i=1:length(s.subs)
if strcmp(s.subs{i}, ':')
s.subs{i} = [];
end
end
b=a.getSubByIdx(s.subs{:});
varargout{1} = b;
case '.'
try
[varargout{1:nargout}] = builtin('subsref', a, s);
catch, rethrow(lasterror); end
end
1 Comment
Matt J
on 28 Mar 2013
Edited: Matt J
on 28 Mar 2013
"Matlab complains" is not enough information. Paste the entire error message from the command window, with info about the line number and statement where it occurred.
Also show the statement you use to call susbref. Why is nargout=3 if you're only asking for 1 output argument?
Answers (0)
See Also
Categories
Find more on Customize Object Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!