Getting Coordinate input from user

3 Ansichten (letzte 30 Tage)
Morteza Ghafoori
Morteza Ghafoori am 16 Okt. 2018
Bearbeitet: Morteza Ghafoori am 19 Okt. 2018
I want to get several point 2d coordinates from the user and store the x and y coordinates in separate arrays. I'm using a for loop to do it but it doesn't assign the input to the variables, here's how I'm doing it:
for i=1:5
[x(i),y(i)]=input('Coordinates of Node')
end
  3 Kommentare
Morteza Ghafoori
Morteza Ghafoori am 16 Okt. 2018
it just gives an error of too many output arguments
Jan
Jan am 16 Okt. 2018
Please post a complete copy of the message in the forum. You see, that the message explains the problem already.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Jan
Jan am 16 Okt. 2018
According to the documentation, |input< has 1 output only:
help input
doc input
Then:
x = zeros(1, 5); % Pre-allocation
y = zeros(1, 5);
for i = 1:5
reply = input('Coordinates of Node')
x(i) = reply(1);
y(i) = reply(2);
end
The pre-allocation is not essential here, because waiting for the user input will take much more time than expanding the arrays x and y. But it is a good programming practice.
  3 Kommentare
Jan
Jan am 16 Okt. 2018
In a separate line? What did you try to get this impression? Maybe you want this:
for i = 1:5
reply = input(sprintf('Coordinates of Node %d: ', i), 's');
num = sscanf(reply, '%g %g');
x(i) = num(1);
y(i) = num(2);
end
Morteza Ghafoori
Morteza Ghafoori am 19 Okt. 2018
Bearbeitet: Morteza Ghafoori am 19 Okt. 2018
Yes, That's exactly what I wanted. Thanks a lot

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by