writing your own linspace() function using nargin checks and not varargin in matlab

Here's an example implementation of linspace function using nargin:

main.m
function y = my_linspace(start, stop, num)

if nargin == 3
    y = linspace(start, stop, num);
elseif nargin == 2
    y = linspace(start, stop);
else
    error('Not enough input arguments');
end

end
200 chars
12 lines

In this implementation, we first check the number of input arguments using nargin. If nargin equals 3, we call the built-in linspace with three input arguments, start, stop, and num. If nargin equals 2, we call linspace with two input arguments, start and stop. If nargin is neither 3 nor 2, we throw an error message with error function.

Note that this implementation intentionally avoids using varargin.

related categories

gistlibby LogSnag