How do I use "inputdlg" box as a prompt to get numbers for multiple variables?

18 views (last 30 days)
Currently, I know that if you put these commands in to an m file:
prompt = {'Enter former year','Enter former measurement','Enter new year','Enter distance from center'};
dlg_title = 'Input for fluke measurement';
yf = inputdlg(prompt,dlg_title)
I will get multiple numbers for the same variable, "yf". What I would like to do is use a dialog box in order to get numbers for multiple variables. So 'Enter former year' would ask for a number corresponding to the variable "yf" and 'Enter former measurement','Enter new year', and 'Enter distance from center' would ask for numbers corresponding to the variables "f", "yn", and "d" respectively. Is there a way to make a dialog box that does this?
Thanks for your help.

Answers (1)

Walter Roberson
Walter Roberson on 17 Jan 2012
In the code you have above, the output yf will not exactly be "multiple numbers for the same variable": it will be a cell array of strings with one string per prompt that you used. You can extract the individual entries as you want.
If you really want them split in to variables, then:
temp = inputdlg(prompt,dlg_title);
[yf, f, yn, d] = temp{:};
Note: that syntax will not work on sufficiently old versions of MATLAB; the older syntax is
[yf, f, yn, d] = deal(temp{:});
  2 Comments
Liz
Liz on 19 Jan 2012
it worked, except if one of the variables contains more than a one-digit number, the "number" is changed to a an array. is there a way to prevent that? the reason I ask is that I would like to use these variables for functions later in the program. Ex: I would like "y=yn-yf" to work out as "y=57-45" to come out as "12" and not "1 2".
Walter Roberson
Walter Roberson on 19 Jan 2012
As I replied in your Question on this subject, http://www.mathworks.com/matlabcentral/answers/26548-how-do-i-ask-for-a-number-instead-of-an-array
No, inputdlg() always returns a cell array of strings. If you want to interpret them as numbers you have to convert them to numbers, such as with str2double()

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!