elenasto
elenasto

Reputation: 23

Is there a way way to generate a custom error message using narginchk in matlab?

Matlab is removing narchk function in future releases and I am trying to change some code to use narginck instead. Now with nargchk, the output was a string and I could pass it to an if statement to display my own error message. Something like

if ~isempty(nargchk(min, max, nargin))
  error('custom error message')
end

narginchk automatically gives an error not a string, so I was wondering if there is a way to give a custom error message with narginchk

Upvotes: 1

Views: 169

Answers (1)

Suever
Suever

Reputation: 65460

You cannot supply a custom error message to nargchk and related functions.

There is no need to use nargchk in your case since you don't need default values or anything, just simply check the value of nargin.

if nargin > max || nargin < min
    error('custom error message');
end

Alternately, you could use assert to eliminate the if statement.

assert(nargin <= max && nargin >= min, 'Custom Error Message');

If you really want to use one of those functions, you could wrap it inside of a try/catch statement and provide a custom error message

try
    narginchk(min, max, nargin)
catch ME
    throw(MException(ME.identifier, 'my custom message'))
end

Upvotes: 1

Related Questions