Reading specific rows and columns of numerical values from a text file
3 views (last 30 days)
Show older comments
I am trying to read specific numerical values from this text file into matlab. I want the values from columns RetTime[min] and Area[pA*s]. Originally I was trying to use the dlmread function, but was running into an error. The error message was "Mismatch between file and format character vector". I believe this is from the non-numerical data at the beginning of the file. After doing some reading the textscan function may be better for this case, but I am struggling with formatting aspect of this function. I don't understand how to read a specific row and column using the textscan function. However, I am unsure if this would even be the best function to use in this case. Using the textscan function, how would I specify a certain column and row to read? Or would there be a better function to use in this case?
2 Comments
per isakson
on 20 Dec 2017
Which release do you use? FixedWidthImportOptions, Import options object for fixed-width text files was Introduced in R2017a
Walter Roberson
on 20 Dec 2017
Note: the file is written as UTF16LE with Byte Order Marker.
per's mention of fixed width is a good point: some of the columns are empty, and there is no delimiter such as comma or tab between the columns.
Answers (2)
Image Analyst
on 20 Dec 2017
Because you have three separate chunks of data separated, before and after, by some other non-matrix stuff, I think you may need to write your own reader to handle the peculiarities of this type of file format. For example, you may want a structure array where each structure has the numerical data along with non-numerical data in fields. So for that file you'd have 3 structures in your structure array, while for another data file there may be 4 or 12 or whatever.
0 Comments
per isakson
on 20 Dec 2017
Edited: per isakson
on 20 Dec 2017
Here is an implementation along the lines suggested by @ImageAnalyst. The code to read the text file is copied from @Walter Roberson. The code is tested on R2016a.
Run cssm
>> out = cssm
out =
1x3 struct array with fields:
Header
Peak
RetTime
Type
Width
Area
pArea
Name
and inspect the result (I use the transpose-blip to save screen area)
>> out(1).RetTime'
ans =
Columns 1 through 8
1.4520 2.4040 3.2870 5.1550 5.5540 6.6860 6.7760 7.0180
Columns 9 through 16
7.8000 7.8550 7.9550 7.9920 8.0480 8.3350 9.1410 9.2370
Columns 17 through 18
10.3690 10.7530
>> out(2).Type'
ans =
'' 'BB' 'BV' 'VB' '' 'BB' '' '' 'BB' 'BB' '' ''
>> >> out(3).Area'
ans =
165.0026 0 42.9935 0 0 674.3498 0
where
function out = cssm()
warning( 'off', 'MATLAB:iofun:UnsupportedEncoding' )
fid = fopen( 'h:\m\cssm\report2.txt', 'rt', 'n', 'UTF16LE' );
warning( 'on', 'MATLAB:iofun:UnsupportedEncoding' )
[~] = fread( fid, 2, '*uint8' );
str = fread( fid, [1,inf], '*char' );
fclose( fid );
str(str==char(13))=[]; % Unexpected: "\r\n" despite "rt" in fopen
xpr = '(?m-s)^Signal \d: [A-Z]{3}\d [A-Z].+$'; % matches first line of block
[ body, header ] = strsplit( str, xpr, 'DelimiterType','RegularExpression' );
body(1) = [];
clh = {'Peak','RetTime','Type','Width','Area','pArea','Name'};
spc = {'%f','%f','%s','%f','%f','%f','%s'};
out = struct( 'Header', header, clh{1},{[]}, clh{2},{[]}, clh{3},{''} ...
, clh{4},{[]}, clh{5},{[]}, clh{6},{''}, clh{7},{''} );
%
delimiter = ',';
frmtspec = '%4c%8c%7c%8c%11c%9c%21c%*[^\n]';
for jj = 1 : length( body )
tok = strsplit( body{jj}, 'Totals' );
tok = strtrim( tok{1} );
buf = textscan( tok, frmtspec, 'Headerlines',3 ...
, 'Whitespace','', 'Delimiter','' );
for ii = 1 : length( clh )
str = cat( 2, char(buf(ii)), repmat( delimiter, [size(buf{ii},1),1] ) );
str = permute( str, [2,1] );
cac = textscan( str, spc{ii}, 'Delimiter',delimiter );
if strcmp( spc(ii), '%s' )
out(jj).(clh{ii}) = strtrim( cac{:} );
else
out(jj).(clh{ii}) = cac{:};
end
end
end
end
... left as an exercise ... ;-)
0 Comments
See Also
Categories
Find more on Large Files and Big Data in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!