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)
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. :)

Answers (1)

Image Analyst
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
Krishna
Krishna on 7 Jul 2014
Thank you for taking the time to write this code! I appreciate it. :) But, this code lets me enter values less than 0! :O
Also, I don't know how I can reference the product name in the dialogue box.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!