Stop matlab crashing when function input is not a number
Show older comments
I've tried isnan(), isnumeric() and the code below, yet whenever I enter L as a letter, Matlab crashed and returns and error. Why does the below code not work?
function [x]= functionname(L,C,R,V,F,State)
if isa(L,'numeric')
Do code
else
x='Error'
Answers (1)
Image Analyst
on 13 Feb 2013
Perhaps this:
% Ask user for a number.
defaultValue = 45;
titleBar = 'Enter a value';
userPrompt = 'Enter the integer';
caUserInput = inputdlg(userPrompt, titleBar, 1, {num2str(defaultValue)});
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
integerValue = round(str2double(cell2mat(caUserInput)));
% Check for a valid integer.
if isnan(integerValue)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
integerValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', integerValue);
uiwait(warndlg(message));
end
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!