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 件のコメント

Jan
Jan 2018 年 10 月 16 日
it doesn't assign the input to the variables
Please do not explain, what the code does not do, but mention, what the code does do. An exact description contains a hint of the way to solve the problem usually.
Morteza Ghafoori
Morteza Ghafoori 2018 年 10 月 16 日
it just gives an error of too many output arguments
Jan
Jan 2018 年 10 月 16 日
Please post a complete copy of the message in the forum. You see, that the message explains the problem already.

サインインしてコメントする。

 採用された回答

Jan
Jan 2018 年 10 月 16 日

0 投票

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 件のコメント

Morteza Ghafoori
Morteza Ghafoori 2018 年 10 月 16 日
this actually works but then the user has to input each coordinate in a separate line. I want the user to input 2 numbers at once but assign each to a separate variable, Is this doable?
Jan
Jan 2018 年 10 月 16 日
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 2018 年 10 月 19 日
編集済み: Morteza Ghafoori 2018 年 10 月 19 日
Yes, That's exactly what I wanted. Thanks a lot

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2018 年 10 月 16 日

編集済み:

2018 年 10 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by