The file is like
2
0.5 0.6 0.7
0.0 -2.5 6.3
The first line is the number of points. The others are the coordinates of those points x y z.
In C++, we have
ifstream fin("coordinate.txt");
fin>>num;
for(int i=0; i<num; i++)
{
fin>>array[i][0]>>array[i][1]>>array[i][2];
}
What are the corresponding code in MATLAB?

 採用された回答

Matz Johansson Bergström
Matz Johansson Bergström 2015 年 1 月 23 日

0 投票

It should be sufficient to write
filename='coordinates.txt';
[x,y,z] = dataread('file', filename, '%f%f%f', 'delimiter',...
' ', 'headerlines', 1);

4 件のコメント

Joe kiao
Joe kiao 2015 年 1 月 23 日
Thank you! But I do not think it will work for my question.
Matz Johansson Bergström
Matz Johansson Bergström 2015 年 1 月 23 日
編集済み: Matz Johansson Bergström 2015 年 1 月 23 日
I did try it and it works on your example. Why do you think it will not work? Please note, the coordinates will be in the vectors x, y and z. If you want to store it in a matrix "array", you could simply write
array = [x,y,z];
Joe kiao
Joe kiao 2015 年 1 月 23 日
Thanks Matz. But you know the above file is just an demonstration. So my question is: is there a command in MATLAB that will read the data one by one and it will automatically goes to a new line when comes to the end of one line?
Matz Johansson Bergström
Matz Johansson Bergström 2015 年 1 月 23 日

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeResizing and Reshaping Matrices についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by