How can I get a prompt to repeat for a user given input?

5 visualizzazioni (ultimi 30 giorni)
Hello!
I'm pretty new to MATLAB, and I was wondering how to write a code that would repeat a prompt and save each answer to a new variable.
For example, I want the user to answer the question 'How many states are you going to?' using the prompt:
states = inputdlg('How many states are you going to?');
states = str2num(states{:});
I then want the question 'How many days are you spending in state 1?' to come up using:
days(1)= inputdlg('How many days are you spending in state (1)?');
Which would repeat for state 1: states. Im using the answer from the number of days per state further along in the code to later get a total amount of money spent, which I can figure out. I'm just a little stuck here.
Thanks!

Risposte (1)

bio lim
bio lim il 6 Lug 2015
Modificato: bio lim il 15 Lug 2015
Hi, Liz. I think you should use sprintf.
states = inputdlg('How many states are you going to?');
states = str2num(states{:});
for i = 1 : states
st = sprintf('How many days are you spending in state %d?', i);
days(i) = inputdlg(st)
end
For example, I put my states as 3 and days as 1, 2 and 4.
days =
'1' '2' '4'
To access the element,
days(1)
ans =
'1'
Hope it helps.

Categorie

Scopri di più su Entering Commands in Help Center e File Exchange

Prodotti

Community Treasure Hunt

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

Start Hunting!

Translated by