Stop matlab crashing when function input is not a number
Ältere Kommentare anzeigen
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'
Antworten (1)
Image Analyst
am 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
Kategorien
Mehr zu Data Type Identification finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!