Reading data from a large text file into matrix

3 views (last 30 days)
Hello, so I am trying to read data from a really large text file and put it into a matrix. I have 2 problems:
1) Using a test file that only has 3 lines of the large text file, I am able to get the data into a matrix, but I can't use the data because they're all cell arrays? I want to be able to use the numbers and strings I read from the large text file to do calculations. This is the sample text file:
N080602ABX 000000BARRICK GOLD CORP 067901108 M
Q080602ABX 00172500399200040350010006
Q080602ABX 07005200400000040350002006
The data is grouped such that I need to separate parts of each line to get what I want, e.g. 001725, 39920, 40350, and so on.
This is my code:
function [ C nLines ] = ret( )
% Returns C matrix with the data
fileID = fopen('ABX.txt');
nLines = 0; %get total number of lines to specify dimensions of matrix later
while (fgets(fileID) ~= -1),
nLines = nLines+1;
end
fclose(fileID);
fileID = fopen('ABX.txt');
C = textscan(fileID, '%1s %6s %*8s %6s %7s %7s %*[^\n]');
% C = textscan(fileID, '%1s %6d %*8c %6d %7d %7d %*[^\n]');
% I didn't use the above line because I couldn't access the individual cells
% of the matrix without reading the text file as all strings
fclose(fileID);
A = C{1};
B = C{2};
D = C{3};
E = C{4};
F = C{5};
C = [A B D E F];
%I couldn't access the individual cells of the matrix without doing this
end
2) Also, when I run my code on the large text file, matlab crashes. Is there anything I can do to clean up my code?

Answers (1)

per isakson
per isakson on 3 Nov 2012
Edited: per isakson on 3 Nov 2012
This code reads the second and third lines
>> cac = cssm()
cac =
[2x1 char] [2x6 char] [2x1 uint32] [2x1 uint32] [2x1 uint32]
>>
where
function cac = cssm()
12345678
Q080602ABX 00172500399200040350010006
frm = '%1c%6c%*8c%6u%7u%7u%*[^\n]';
fid = fopen( 'cssm.txt', 'r' );
cac = textscan( fid, frm, 'Delimiter', '' ...
, 'Whitespace', '', 'Headerlines', 1 );
fclose( fid );
end
and cssm.txt contains your three sample lines.
You need to handle the first line separately in one way or another.
.
--- in response to comment ---
>> [ txt, num ] = cssm()
txt =
Q
Q
num =
80602 1725 39920 40350
80602 70052 40000 40350
>> num(2,2)
ans =
70052
>> sum( num )
ans =
161204 71777 79920 80700
where
function [ txt, num ] = cssm()
cac = cssm_();
txt = cac{1};
num = [ cac{2}, cac{3}, cac{4}, cac{5} ];
end
function cac = cssm_()
12345678
Q080602ABX 00172500399200040350010006
frm = '%1c%6f%*8c%6f%7f%7f%*[^\n]';
fid = fopen( 'cssm.txt', 'r' );
cac = textscan( fid, frm, 'Delimiter', '' ...
, 'Whitespace', '', 'Headerlines', 1 );
fclose( fid );
end
  2 Comments
Louise
Louise on 3 Nov 2012
Hi, my problem is that cac is now a 1x5 matrix and I don't know how to access the individual elements in each row, e.g 70052
per isakson
per isakson on 3 Nov 2012
Edited: per isakson on 3 Nov 2012
You have to separate text, e.q. "Q", from the "numerical" strings.
Either, you read the file as text and convert to numeric in a second step or you do it in one step with the help of conversion specifiers in the format string.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!