フィルターのクリア

Reading text file using fprintf and textscan

7 ビュー (過去 30 日間)
Danupon Subanapong
Danupon Subanapong 2019 年 4 月 5 日
Hello!!
I am reading a text file and trying to extract array from the file. I used script as below.
fid=fopen('test.txt');
B_a=textscan(fid,'%f\t%f\t%f\t%f\t%f\r\n','HeaderLines', 1);
fclose(fid);
The goal is to acheive a matrix as below.
B_a=[0.050000,6999.282209,-791.957055,6644.515337,2338.276074;
0.150000,8999.282209,-751.957055,6844.515337,2339.276074]
But as I did I only get an array contains only the first line of text file.
Can anyone please help me?

採用された回答

Guillaume
Guillaume 2019 年 4 月 5 日
You could continue to use textscan, you don't specify line endings and separators in the format string:
fid = fopen('test.txt');
B_a=textscan(fid,'%f%f%f%f%f','HeaderLines', 1);
fclose(fid);
B_a = cell2mat(B_a);
You could also use readtable to read as a table, then convert to a matrix. If the header of the your file made more sense, readtable could have used that to name the variables.
B_a = table2array(readtable('test.txt'));
If on the newly released R2019a, the simplest is to use readmatrix:
B_a = readmatrix('test.txt'); %all done. It figures out the number of headerlines on its own.
  1 件のコメント
Danupon Subanapong
Danupon Subanapong 2019 年 4 月 5 日
Thank you so much!! It helps a lot

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeText Files についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by