MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Call testValues with too few inputs.


testValues(10)


Error using testValues (line 4)
Not enough input arguments.


Call testValues with enough inputs.


testValues(10,1,11,111)


Test value 2 exceeds 10
Test value 3 exceeds 10


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 =


Validate Number of Function Arguments
Free download pdf