Converting a text file into strings and integers

2 ビュー (過去 30 日間)
Paolo Binetti
Paolo Binetti 2016 年 12 月 19 日
コメント済み: Paolo Binetti 2016 年 12 月 21 日
I have .txt input files with a very simple structure, of which I attach an example. I want to convert its content in strings and integers. My code below works, but I was wondering if there is a faster option than "textread", which seems to be the weak point.
[pattern, text, d] = textread('dataset.txt', '%s %s %d');
pattern = cell2mat(pattern(1));
text = cell2mat(text(1));
  2 件のコメント
Paolo Binetti
Paolo Binetti 2016 年 12 月 19 日
that textread is not recommended, but it does not say why and textscan does not seem to work in the same way. I have actually tried implementing textscan but I did not figure it out. Amazing how much time one can lose with I/O ...
Walter Roberson
Walter Roberson 2016 年 12 月 20 日
textscan is more flexible.

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

採用された回答

Walter Roberson
Walter Roberson 2016 年 12 月 19 日
Is there only the one set of values for each file? If so then probably a few fscanf() would be faster.
  3 件のコメント
Walter Roberson
Walter Roberson 2016 年 12 月 20 日
fid = fopen('dataset.txt');
pattern = fscanf(fid, '%s', 1);
txt = fscanf(fid, '%s', 1);
d = fscanf(fid, '%f', 1);
fclose(fid);
Paolo Binetti
Paolo Binetti 2016 年 12 月 21 日
Thank you: 5 times faster on my sample, noticeably faster on larger files, no longer the slowest piece of code in my algorithm.

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

その他の回答 (0 件)

カテゴリ

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

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by