How do I use fscanf to read correctly?

1 回表示 (過去 30 日間)
Alexandra Wu
Alexandra Wu 2021 年 9 月 7 日
編集済み: Piotr Balik 2021 年 9 月 7 日
Hi,
I am trying to use fscanf f to read a txt file.
I want
1 0.0 0.0
2 0.0 120.0
3 120.0 0.0
4 120.0 120.0
However when I put in my code
data = fscanf (inp, "%d %f %f" , [nNode,3]);
This is what shows up:
1 0 0
0 120 4
0 3 120
2 120 120.
Thank you so much!

回答 (1 件)

Piotr Balik
Piotr Balik 2021 年 9 月 7 日
編集済み: Piotr Balik 2021 年 9 月 7 日
Fscanf tries to read whole file at once, but does output it along rows, hence your problem occurs. You could try other maneuvers, but this simple code assumes you know how big is your file
for ii=1:nNode
data(ii,:) = fscanf (inp, "%d %f %f" , [1,3])
end
fclose(inp)

カテゴリ

Help Center および File ExchangeGet Started with MATLAB についてさらに検索

タグ

製品


リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by