MATLAB: How to get integer data using inputdlg,, or using any other way

getting decimal value using dialogue box

Hi Everyone, I am using the following command to get data using dialog box.
prompt={'Enter Lowest Energy'};
dlg_title='Input';
lowest_energy1=inputdlg(prompt,dlg_title);
disp(lowest_energy1);
In this I am giving the decimal value as an input. but i can't use it as a decimal value. the value of lowest_energy1= '1'(if i write 1 in the dialogue box).
that can not be used as decimal value. Kindly help me in this regard. Thanks in anticipation 🙂

Best Answer

Here's a way to force the user to enter an integer:
% 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.
% Round to nearest integer in case they entered a floating point number.
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
Related Question