Output Checks with nargoutchk
Define a function in a file named mysize.m that returns the dimensions of the input array in a vector
(from the size function), and optionally returns scalar values corresponding to the sizes of each
dimension. Use nargoutchk to verify that the number of requested individual sizes does not exceed
the number of available dimensions.
function [sizeVector,varargout] = mysize(x)
minOutputs = 0;
maxOutputs = ndims(x) + 1;
nargoutchk(minOutputs,maxOutputs)
sizeVector = size(x);
varargout = cell(1,nargout-1);
for k = 1:length(varargout)
varargout{k} = sizeVector(k);
end
Call mysize with a valid number of outputs.
A = rand(3,4,2);
[fullsize,nrows,ncols,npages] = mysize(A)
fullsize =
3
4
2
nrows =
3
ncols =
4
npages =
2
Call mysize with too many outputs.
A = 1;
[fullsize,nrows,ncols,npages] = mysize(A)
Error using mysize (line 4)
Too many output arguments.
See Also
narginchk | nargoutchk
Related Examples
•
“Support Variable Number of Inputs” on page 21-4
•
“Support Variable Number of Outputs” on page 21-5
Validate Number of Function Arguments
21-7
Précédent

- 763/1450

Suivant