How to skip [] when add values to a vector variable?

How to avoid having to put [] around the values that are going into the vector variable when using input command?
for i = 1:n;
values=input('Enter a value [x y]: ');
point=[point; values]
end
Enter a point [x y]: 2 2
Error using askpoints (line 7)
Error: Invalid expression. Check for missing multiplication operator, missing or
unbalanced delimiters, or other syntax error. To construct matrices, use brackets
instead of parentheses.

回答 (2 件)

madhan ravi
madhan ravi 2018 年 12 月 5 日
編集済み: madhan ravi 2018 年 12 月 5 日

0 投票

% Just alter the below lines in your code
points=cell(1,n); % before loop
points{i}=values; %inside loop
cell2mat(points) %outside loop

5 件のコメント

Fille
Fille 2018 年 12 月 5 日
Hi,
Thanks for the quick answer.
Still some issue
part of the function
points=cell(1,n);
for i = 1:n;
values=input('Enter a point [x y]: ');
points{i}=values;
i=i+1;
end
cell2mat(points)
Enter a point [x y]: 2 2
Error using function (line 4)
Error: Invalid expression. Check for missing multiplication operator, missing or
unbalanced delimiters, or other syntax error. To construct matrices, use brackets
instead of parentheses.
Fille
Fille 2018 年 12 月 5 日
Added 's'
values=input('Enter a point [x y]: ','s');
madhan ravi
madhan ravi 2018 年 12 月 5 日
give input as shown
[2 2]
reshape([points{:}],[],2) % change last line to this also remove i=i+1;
Fille
Fille 2018 年 12 月 6 日
Hi,
Have realized that I have to have a matrix instead of vector. ie every input must be on own line
Example:
1 1 input 1
2 2 input 2
3 3 input 3
Do I have to use another way of converting string to the values to the matrix?
madhan ravi
madhan ravi 2018 年 12 月 6 日
values=input('Enter a point [x y]: ')*[ones(1,2)]

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

Stephen23
Stephen23 2018 年 12 月 5 日
編集済み: Stephen23 2018 年 12 月 5 日

0 投票

str = input('Enter values x y: ','s');
vec = sscanf(str,'%f')

カテゴリ

ヘルプ センター および File ExchangeOperators and Elementary Operations についてさらに検索

タグ

質問済み:

2018 年 12 月 5 日

コメント済み:

2018 年 12 月 6 日

Community Treasure Hunt

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

Start Hunting!

Translated by