MATLAB Object-Oriented Programming

(Joyce) #1

Number of Arguments for subsref and subsasgn


In this section...
“How MATLAB Determines Number of Arguments” on page 17-6
“Syntax for subsref, and subsasgn Methods” on page 17-8

How MATLAB Determines Number of Arguments


MATLAB calls subsref or subsasgn to determine the result of executing code that
involves indexed reference or assignment. The number of elements referenced or
assigned by an indexing operation determines the number of arguments MATLAB uses to
call subsref and subsasgn. That is, the indexing code determines the number of
arguments that MATLAB:


  • Returns from the call to subsref

  • Passes to the call to subsasgn


Therefore, the indexing code determines the value of nargout for the call to subsref
and the value of nargin for the call to subsasgn.

For example, consider the ValuesArray class.

classdef ValuesArray
properties
Values
end
methods
function obj = ValuesArray(v)
if nargin > 0
obj.Values = v;
end
end
end
end

Create an array of 10 ValuesArray objects.

l = ValuesArray.empty;
for k = 1:10
l(k) = ValuesArray(k);
end

17 Specialize Object Behavior

Free download pdf