matlab - How can I report custom errors when a function call is missing some arguments? -



matlab - How can I report custom errors when a function call is missing some arguments? -

i'm teaching myself basics of matlab, , i'm stuck on how create errors functions. here attempt:

function kinetic = ke(m,v) ke = 0.5*m*v*v %error messages if (isempty(m)) % mass empty error('no mass given (argument 1)'); elseif (isempty(v)) % velocity empty error('no velocity given (argument 2)'); end fprintf('the kinetic energy %d joules\n', ke);

so if user doesn't specify 2 variables, function gives error telling user variable didn't specify. when seek error message, matlab returns generic error message:

kinetic(,3) kinetic(,3) | error: look or statement incorrect--possibly unbalanced (, {, or [.

i don't know how prepare this. i've tried replacing arguments of isempty arg1 or arg2, made no difference. tried copying illustration code @ http://www.mathworks.co.uk/help/matlab/ref/error.html still didn't help.

how generate specific errors functions of several variables?

i know quite basic question, help appreciated.

there few problems code:

the syntax of function signature is

(square brackets optional if function has 1 output.) here, kinetic output of function, whereas ke name of function; therefore, phone call function has form

ke(m,v)

not

kinetic(m,v)

the isempty function meant observe whether array (and in matlab, 2d array, default) empty or not. can't utilize observe whether function phone call missing arguments.

as pointed out oliver, ke(,v) not right matlab syntax, , matlab stop in tracks , allow user know of blunder before attempts process function call.

what want do, here, define variadic function, i.e. function can take varying number of arguments. utilize varargin , nargin that; more details, in matlab help.

finally, want to

vectorize function, i.e. create compatible vector inputs, using entrywise operators (.* , .^), prevent results beingness printed in command window, terminating assignment statements semicolon. function kinetic = ke(varargin) if nargin == 0 error('no mass or velocity given') elseif nargin == 1 error('no velocity given (argument 2)') elseif nargin == 2 m=varargin{1}; v=varargin{2}; else error('too many inputs') end ke = 0.5*m.*v.^2; fprintf('the kinetic energy %d joules\n', ke)

matlab error-reporting variadic-functions

Comments

Popular posts from this blog

formatting - SAS SQL Datepart function returning odd values -

c++ - Apple Mach-O Linker Error(Duplicate Symbols For Architecture armv7) -

php - Yii 2: Unable to find a class into the extension 'yii2-admin' -