How to read strings from a txt file?

9 ビュー (過去 30 日間)
Benson Gou
Benson Gou 2020 年 9 月 18 日
コメント済み: Benson Gou 2020 年 9 月 18 日
Dear All,
I need to read strings from a txt file which contains numerical data and strings. The file content is as follows:
model = [
1.23 45.6 789.0 abc
234.0 56.7 8.91 def
345.0 678.0 9.12 ghi
];
How can I read the strings and save them in a file called NameList?
Thanks in advance.
Benson

採用された回答

per isakson
per isakson 2020 年 9 月 18 日
編集済み: per isakson 2020 年 9 月 18 日
The text file, model.txt, contains the five lines given in your question.
The script
fid = fopen( 'model.txt' );
cac = textscan( fid, '%f%f%f%s', 'Headerlines',1 ...
, 'CommentStyle',']', 'CollectOutput', true );
fclose( fid );
reads that file. Inspect the output
>> cac{:}
ans =
1.23 45.6 789
234 56.7 8.91
345 678 9.12
ans =
3×1 cell array
{'abc'}
{'def'}
{'ghi'}
>>
This is one way to do it.
  1 件のコメント
Benson Gou
Benson Gou 2020 年 9 月 18 日
Thanks a lot for your great help.
Benson

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeString Parsing についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by