How to convert text file with different delimiters into a matrix

2 views (last 30 days)
|Hi,
My text file contains 60K lines with format:| | |
0 qid:10 1:0.000000 2:0.000201 3:0.10090 4:0.0043000 5:0.1900000.. 46:0.650000 #docid = GX000-00-000000
Now I have to load data from this file onto a matrix with column '1' and column '3' to column '48' values ignoring value before colon.
O/P: [0 0.000000 0.000201 0.10090 0.0043000 0.1900000...0.650000] //this is single row in matrix
I tried this:
fid = fopen('Querylevelnorm.txt');
--for loop for each line begins here
tline = fgetl(fid)
C=strsplit(tline)
vector = [];
--for loop for each value in line starting from C(1) then C(3) to C(48)
D=strsplit(char(C(3)),':')
vector=[vector D(2)] ##formulating of vector
--end for
--adding present vector to matrix ## formulating of matrix
--end for
i.e., I am reading each line and first splitting with space and then with colon to get each value. But I am not getting how to populate all these values into a vector and then adding each vector to a matrix. I am new to this matlab programming. Sorry for any wrong editing. Please help me out.

Accepted Answer

dpb
dpb on 5 Oct 2014
Edited: dpb on 5 Oct 2014
Use textscan and a specific format string...
>> l='0 qid:10 1:0.000000 2:0.000201 3:0.10090 4:0.0043000 5:0.1900000 46:0.650000 #docid = GX000-00-000000';
>> fmt=['%f %*s' repmat('%*d:%f',1,6) '%*[^\n]'];
>> cell2mat(textscan(l,fmt,'collectoutput',1))
ans =
0 0 0.0002 0.1009 0.0043 0.1900 0.6500
>>
The format string translates as--
a) read first decimal value (use float to collect all into one array in end)
b) skip first string field (the 'quid:value' will be a single string)
c) read the next sequence of [skipdecimal:readfloat] N times
d) skip rest of line...
See
doc textscan for the details.
NB: use 46 for the repeat count in repmat for your case; 6 matched the length of the line cleaned up to remove the ellipses you posted.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!