Reading big textfile with textscan

7 ビュー (過去 30 日間)
David Pfläging
David Pfläging 2015 年 7 月 20 日
編集済み: Stephen23 2015 年 7 月 20 日
Hi,
I want to read a big textfile (~5MB) with textscan. The first 10 rows contain some informations which I don't need. Then 1024 rows with 1280 numbers per row follow. The numbers are separated by a comma. I found almost the right code except that each comma induces a new row instead separating the numbers. To use this code I have to delete the first 10 rows in my file.
fid = fopen('textfile.txt')
Out = textscan(fid,'%f','delimiter',sprintf(','));
fclose(fid)
How should I manipulate the code so that I get a 1024x1280 array? I tried
sprintf('\n')
instead but that just gives me the first number and then stops.

採用された回答

Stephen23
Stephen23 2015 年 7 月 20 日
編集済み: Stephen23 2015 年 7 月 20 日
Solution One
You do not need to delete the first ten rows from the file, because textscan has an option to ignore header lines, so you can use this instead:
Out = textscan(....,'HeaderLines',10);
To read 1280 columns of data you need to define this in the format string, so the final code could be something like this:
fmt = repmat('%f',1,1280);
fid = fopen('textfile.txt','rt')
out = textscan(fid, fmt, 'Delimiter',',', 'HeaderLines',10);
fclose(fid);
Solution Two
If the data in the matrix is all numeric (note the header lines do not need to be) then you could use the much simpler dlmread instead, something a bit like this:
out = dlmread('textfile.txt',',',10,0);
Examples
% textscan
fmt = repmat('%f',1,5);
fid = fopen('temp.txt','rt');
out_txt = textscan(fid, fmt, 'Delimiter',',', 'HeaderLines',3);
fclose(fid);
out_txt = cell2mat(out_txt)
% dlmread
out_dlm = dlmread('temp.txt',',',3,0)
display this in the command window:
out_txt =
0 1 2 3 4
5 6 7 8 9
out_dlm =
0 1 2 3 4
5 6 7 8 9
The sample text file that I used is attached here:
  1 件のコメント
David Pfläging
David Pfläging 2015 年 7 月 20 日
It works perfectly! Thank you very much!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLarge Files and Big Data についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by