MATLAB: How to take a number as input

numeric inputoutputsimple input

I am, writing a code in which the user is aked to input a value. the value should be restricted to numbers only and no string. if the user enters anything other than a number error message should be displayed

Best Answer

You can create an input dialog box using answer = inputdlg(prompt,dlgtitle) (see that page for additional input options). The output will be a cell array of characters (or an empty cell array).
Convert the output to numeric
answerNum = str2double(answer);
Use a conditional error enforcing numeric input
if isempty(answerNum) || isnan(answerNum) % || ~isnumeric(answerNum)
error('Input must be numeric.')
end