How do I create a dialog-box to work with numerical values?

29 views (last 30 days)
I'd like let matlab display a dialog box to obtain information which is needed to do certain calculations. So I created a dialog box like this:
prompt = {'value 1:','value 2:'};
answer = inputdlg(prompt);
fprintf('value 1 = %d value 2 = %d', answer(1), answer(2))
The problem is; you get your information in strings this way but you can't calculate with these. So I tried to solve this problem by using 'str2num(answer(1))' instead of just 'answer(1)', but then matlab returns an error. How do I solve this? I've searched for threads questioning this problem, and I found similar questions, but I couldn't find the answer I'm looking for. Thanks for your help
Forgot to mention, I'm just a beginner at matlab and I kinda copied this dialog-box code, I don't really know the exact working of all of it's components yet..

Accepted Answer

Bart
Bart on 30 Nov 2014
Nevermind, i figured it out myself already. This does the trick for me:
values = {'value 1:','value 2:'};
values = inputdlg(values);
values = str2double(values);
fprintf('value 1 = %d', values(1))
I just wrote it wrong, thats the problem when you copy stuff you actually don't understand.. :p

More Answers (1)

Image Analyst
Image Analyst on 30 Nov 2014
Try this snippet:
% Ask user for two numbers.
defaultValue = {'45', '67'};
titleBar = 'Enter a value';
userPrompt = {'Enter number 1 : ', 'Enter 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 integer.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
usersValue = defaultValue;
message = sprintf('I said it had to be an integer.\nI will use %d and continue.', usersValue1);
uiwait(warndlg(message));
end
Let me know if that solves the issue for you.
  4 Comments
Image Analyst
Image Analyst on 30 Nov 2014
It was an attempt to show you how to write robust code. Your code is fragile. What happens when your user enters some non-numerical entry? What happens if the user clicks Cancel? Your code will fail and barf up red error text all over the place and then you'll come back here asking how to solve that.
In my job I need to write bulletproof code. I need to anticipate that you may not get exactly the input you were expecting. About half my code is error checking, validating, handling unexpected data, warning users, etc. It would literally be half as long if I didn't check for unexpected inputs or results. But it pays off because I can ship my mission critical code anywhere in the world and I know it won't crash. I either fix the bad data or warn the user to fix it. If I wrote fragile code, I'd probably get fired from my job, seriously. When I write code I think of a question I heard once in a training class: "Would you fly in an airplane that you or your team wrote code for?" That's why I write robust code from the start . Eventually you will too, but it might perhaps take some embarrassing complaints from users. I know a lot of people here hate my code because it's longer and they accept the simpler, less robust, and less flexible solutions offered. That's okay - whatever. It's fine if you prefer something simple for a homework problem where you know you'll input the right thing and even if you don't you can just re-run it. It just usually goes against my nature to offer up fragile code when the robust code is not that more complicated, so that's what I do.
Image Analyst
Image Analyst on 30 Nov 2014
By the way, here's a nice little snippet that forms a nice framework for the contents of a function you might write:
try
% Some code that might generate an error.
catch ME
errorMessage = sprintf('Error in function %s() at line %d.\n\nError Message:\n%s', ...
ME.stack(1).name, ME.stack(1).line, ME.message);
fprintf(1, '%s\n', errorMessage);
uiwait(warndlg(errorMessage));
end
Just put that inside your function and put your code after the "% Some code" part and you'll get a nice popup error message that won't crash your program that will tell you what function and line your code had a problem with.

Sign in to comment.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!