Converting a text file into strings and integers

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

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 ...
textscan is more flexible.

댓글을 달려면 로그인하십시오.

 채택된 답변

Walter Roberson
Walter Roberson 2016년 12월 19일

1 개 추천

Is there only the one set of values for each file? If so then probably a few fscanf() would be faster.

댓글 수: 3

Thank you but I am not sure I understand your question. I have attached a file example. That is exactly the file I want to read (except that the string on the second row would be much longer).
fid = fopen('dataset.txt');
pattern = fscanf(fid, '%s', 1);
txt = fscanf(fid, '%s', 1);
d = fscanf(fid, '%f', 1);
fclose(fid);
Thank you: 5 times faster on my sample, noticeably faster on larger files, no longer the slowest piece of code in my algorithm.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

질문:

2016년 12월 19일

댓글:

2016년 12월 21일

Community Treasure Hunt

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

Start Hunting!

Translated by