Using textscan with txt

1 回表示 (過去 30 日間)
Jake
Jake 2016 年 4 月 25 日
コメント済み: Jake 2016 年 5 月 2 日
Hi MATLAB Experts,
I am trying to use textscan function to read txt file, which is actually having more than 100,000 rows and 100 columns.
In order to convert data into a matrix in MATLAB, I am using for loops as follows:
fid = fopen(filename,'r');
tmp = textscan(fid,'%s','Delimiter','\n'); % there are many different data format existing (i.e. char/cell/double)
USdata = [];
for n = 2:length(tmp{1})
temp_data = regexp(tmp{1}{n},'\t','split');
USdata = [USdata; temp_data];
end
However, it takes too long since it has more than 100,000 rows. What would be the most efficient way to do it??
Thanks for any help you could provide in advance.
Jake

採用された回答

John BG
John BG 2016 年 4 月 26 日
Try just reading row by row
fid=fopen('example_text.txt')
A={}
tline = fgetl(fid)
while ischar(tline)
A=[A; tline]
tline = fgetl(fid)
end
fclose(fid)
If you find this answer of any help solving your question,
please click on the thumbs-up vote link,
thanks in advance John
  1 件のコメント
Jake
Jake 2016 年 5 月 2 日
Hi John,
I tried the way you suggested above. It ran much faster. However, I want to a cell matrix (# of column by # of raw). How can I do it?

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 4 月 25 日
ncol = 100;
fmt = strjoin(repmat({'%[^\t]'},1,ncol),'\t');
fid = fopen(filename,'r');
tmp = textscan(fid, fmt, 'CollectOutput', 1));
fclose(fid);
data = tmp{1};
If you know the column formats, it would be better to construct a more specific fmt. You might want to adapt some of the methods I use in http://www.mathworks.com/matlabcentral/answers/280772-how-can-i-load-target-data-on-student-alcohol-consumption#answer_219273

カテゴリ

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