Reading Text Files with Numbers and String

Hi, this is a common question and I could not find an answer pertaining to my problem
I have attached an excerpt of my textfile in this question
my text file is built like this and I want to extract ID time and C columns, I tried dlmread and textscan but they yield errors, any help appreciated
ID#: 0 time: 6379.937500 A: 0 B: 0.000000 ; C 0.000000
% my current code
fileID = fopen('ni100khz.txt');
A=textscan(fileID, '%s %f %s %f %s %f %s %f %s %s %f %f ');
fclose(fileID);
% it doesn't scan the whole txt file

2 件のコメント

源樹 上林
源樹 上林 2020 年 8 月 13 日
readtable ?
Hans123
Hans123 2020 年 8 月 13 日
Thanks for the reply, I am working with MATLAB 2017 and it is not available for me. I have attached the format of my textfile, hope it helps. I am currently trying textscan but can't make progress

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

 採用された回答

Star Strider
Star Strider 2020 年 8 月 13 日
編集済み: Star Strider 2020 年 8 月 13 日

1 投票

Try this:
fidi = fopen('data1.txt');
A = textscan(fidi, 'ID#: %f time: %f A: %f B: %f C %f', 'Delimiter',{'\t',';'}, 'MultipleDelimsAsOne',1, 'CollectOutput',1, 'EndOfLine','\r\n');
fclose(fidi);
producing:
Am = cell2mat(A)
Am =
100 4.6563 0 0 0
100 4.6875 0 0 0
100 4.6875 614 3.001 30.01
EDIT —
To extract ‘ID#’, ‘time:’ and ‘C’ only:
A = textscan(fidi, 'ID#: %f time: %f A: %*f B: %*f C %f', 'Delimiter',{'\t',';'}, 'MultipleDelimsAsOne',1, 'CollectOutput',1, 'EndOfLine','\r\n');
The rest of the code is unchanged.
.

2 件のコメント

Hans123
Hans123 2020 年 8 月 14 日
works exactly as I want. As always, thank you Star Strider!
Star Strider
Star Strider 2020 年 8 月 14 日
As always, my pleasure!

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

その他の回答 (0 件)

カテゴリ

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

質問済み:

2020 年 8 月 13 日

コメント済み:

2020 年 8 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by