Info
This question is closed. Reopen it to edit or answer.
Need help with extracting a string from a matrix and placing it in an input box
1 view (last 30 days)
Show older comments
Hello there!
I'm working on a small project and I wanted to know how I could use a menu to my advantage.
I want to prompt the user a menu of supermarket products he could buy. And once he selects one, I want to prompt an input box that says 'How many of %s do you want?' (%s being the name of the product he chose) If he says 0, it will go back to the menu and if he enters something wrong, it would prompt an error box.
Forgive me if I'm not technical. This is my first programming language and I'm only 4 weeks into it. Thank you so much for your help! I'd like to work with one of you in person if you are up to help a novice coder with his summer project. :)
0 Comments
Answers (1)
Image Analyst
on 7 Jul 2014
You can try this snippet:
% 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
2 Comments
Image Analyst
on 7 Jul 2014
So just check for that and tell the user:
if isnan(integerValue) || integerValue < 0
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!