I try to check if input argument is of specific type and throw error message like:
function test(input)
if ~ischar(input)
error('%s is invalid input type.', class(input));
end
end
But Matlab shows error message with backtrace
-information:
>> test(1)
Error using test (line 3)
double is invalid input type.
How can I turn off the line Error using test (line 3)
?
I'm looking for something similar to off backtrace
with warning
: warning off backtrace;
.
I'm not sure you can. The closest I got was by defining my own error structure:
testerr.message = 'test';
testerr.identifier = '';
testerr.stack.file = '';
testerr.stack.name = 'Test Thing';
testerr.stack.line = 1;
error(testerr)
Which returns:
Error using Test Thing
test
As long as you keep the file
field blank it will not display the line specified in the stack.
One potential workaround could be a combination of fprintf
and return
, courtesy of Undocumented MATLAB :
function test(input)
if ~ischar(input)
fprintf(2, '%s is invalid input type.\n', class(input));
return
end
end
Depending on where this check resides in your real function you might need to get creative with how it exits, since return
only kicks you back to the invoking function. Probably have it output a True
/ False
flag?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.