How to assign different values to different variables input by the user?

1 vue (au cours des 30 derniers jours)
DIJESH
DIJESH le 26 Mai 2014
Commenté : Mischa Kim le 26 Mai 2014
prompt = 'Enter data: ';
title = 'Data input';
dlg_ans = inputdlg(prompt,title,[1 100]);
data = str2num(dlg_ans{:});
In the above program, the user is allowed to enter as many values as he wish. So how can i assign these different values to different variables automatically as they enter each value?
  1 commentaire
DIJESH
DIJESH le 26 Mai 2014
Modifié(e) : DIJESH le 26 Mai 2014
if i go by the code -
answer1 = data(1)
answer2 = data(2)
this will read each values and assign it to the respective variables but since i do not know the number values entered by the user, how would i manage it to assign it to different variables?

Connectez-vous pour commenter.

Réponse acceptée

Mischa Kim
Mischa Kim le 26 Mai 2014
Modifié(e) : Mischa Kim le 26 Mai 2014
Dijesh, you could use the eval command:
prompt = 'Enter data: ';
title = 'Data input';
dlg_ans = inputdlg(prompt,title,[1 100]);
data = str2num(dlg_ans{:});
for ii=1:numel(data)
eval(sprintf('data%d = %f\n', ii, data(ii)));
end
However, as pointed out in one of your other questions I'd recommend against this approach. See this answer for reference.
  2 commentaires
DIJESH
DIJESH le 26 Mai 2014
May I know why do u suggest against using the above approach? If there is a way why can't we use it?
Mischa Kim
Mischa Kim le 26 Mai 2014
I just noticed that the link I posted above was not working. It is fixed now.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by