How to get integer data using inputdlg,, or using any other way?
31 views (last 30 days)
Show older comments
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 :)
0 Comments
Accepted Answer
Image Analyst
on 27 Apr 2015
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
4 Comments
Image Analyst
on 28 Apr 2015
Here's my snippet for that. Adapt as needed:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check for a valid number.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!