multiple number of inputs n

Hi all,
I need to enter by keyboard some values and them use for loop for them so that matlab should know the number of them automatically.. So, How to enter multiple number of input values (let us say n) seperated with commas or space ? I tried the function input , but it doesn't work for n number of inputs because it include the space or comma as a length of the string
n=input('enter the value of quereis','s')

 Réponse acceptée

Azzi Abdelmalek
Azzi Abdelmalek le 19 Nov 2012

1 vote

n=input('enter the value of quereis','s')
if separated with comma
out=regexp(n,',','split')

Plus de réponses (2)

Harshit
Harshit le 19 Nov 2012

1 vote

it do works. Just give the input in [2 3 4] brackets.

4 commentaires

Jwana
Jwana le 19 Nov 2012
but the length of the input is worng ( it encludes the spaces between each value)
Harshit
Harshit le 19 Nov 2012
No it doesn't include the space. It gives me 3 which is correct and you can access them just as array
Walter Roberson
Walter Roberson le 19 Nov 2012
Note that Jwana used the 's' option of input, which reads input as a string. The 's' option has to be removed for Harshit's answer to work.
Harshit
Harshit le 19 Nov 2012
Thanks Walter. I agree.

Connectez-vous pour commenter.

Walter Roberson
Walter Roberson le 19 Nov 2012

1 vote

values = input('enter the value of queries')
and have the user type input such as
[5 13 9 -5 6]
including the []. The user could include commas.
Or,
valstring = input('enter the value of queries', 's');
valparts = regexp(valstring, '[ ,]', 'split');
values = str2double(valparts);
in which case the user would not need to use [] around the values, and could use space or comma.

1 commentaire

Walter Roberson
Walter Roberson le 19 Nov 2012
Ah, I just re-read the question and see that you want to enter by keywords. In that case, the variable "valparts" above will have the answer. valparts{1} would be the first word, valparts{2} the second, and so on.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by