Prompt depending on another prompt

4 views (last 30 days)
in-p
in-p on 14 Apr 2015
Answered: Guillaume on 14 Apr 2015
Hi. I need a first prompt input that will determinate the size of second prompt... Ex:
1st prompt: 'Number of costumers?' (input)
2nd prompt: 'Name of costumer 1:' 'Name of costumer 2:' and so on.
Any ideas? Thanks

Answers (1)

Guillaume
Guillaume on 14 Apr 2015
This would work:
numcustomers = input('Number of customers ?');
customers = cell{1, numcustomers);
for customeridx = 1:numcustomers
customers{customeridx} = input(sprintf('Name of customer %d:', customeridx), 's');
end
Note, however that I personally dislike prompts like this. I'd rather define a function that just take a cell array of customer names and leave it up to the user to generate that cell array, through prompt as above, or a gui, or by reading a file, or just typing the whole array at the command line.
Imagine if you have to run your script ten times, where each time just one name changes. You still have to enter all the names every time which can get a bit tedious.

Tags

Community Treasure Hunt

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

Start Hunting!