- read the entire file as character
- extracts the "numerical chunks" (an appropriate job for regexp). See Lookahead Assertions in Regular Expressions
- convert to double
How to import a txt file with specific format
1 view (last 30 days)
Show older comments
I need to build the data to construct a report with differents fields like s11 s21 and so on.
I have a text files looks similar to the following:
Frequency / GHz S1,1/abs,linear S1,1/arg,degrees
----------------------------------------------------------------------------------------------------------------
0.5 0.23338409 174.80629
0.5055 0.23355055 174.72296
0.511 0.23371744 174.63905
0.5165 0.23388473 174.55456
Frequency / GHz S2,1/abs,linear S2,1/arg,degrees
----------------------------------------------------------------------------------------------------------------
0.5 0.97077225 314.88972
0.5055 0.97072179 314.39626
0.511 0.97067132 313.90283
0.5165 0.97062083 313.40943
0.522 0.97057033 312.91605
0 Comments
Accepted Answer
per isakson
on 24 Nov 2014
Edited: per isakson
on 24 Nov 2014
If the entire file fits in memory I recommend that you
>> M = cssm()
M =
[4x3 double] [5x3 double]
where cssm.m contains
function M = cssm()
num_xpr = '[ 0-9\.\+\-e\r\n]++';
xpr = ['(?<=([-]{10,}) *[\r\n]+)',num_xpr,'(?=(Frequency)|($))'];
str = fileread( 'cssm.txt' );
cac = regexp( str, xpr, 'match' );
M = cell( 1, length( cac ) );
for jj = 1 : length( cac )
M{jj} = str2num( cac{jj} );
end
end
and cssm.txt contains the example of your question
 
or an alternative implementation:
The magic number 150 is the minimum number of characters that a block of numerical data must contain.
>> g = read_blocks_of_numerical_data( 'cssm.txt', 150 )
g =
[4x3 double] [5x3 double]
where read_blocks_of_numerical_data
function out = read_blocks_of_numerical_data(filespec,block_size,delim)
narginchk( 2, 3 )
buffer = fileread( filespec );
if nargin == 2
del_xpr = '[ ]+';
trl_xpr = '[ ]*';
else
del_xpr = ['([ ]*',delimiter,'[ ]*)'];
trl_xpr = ['([ ]*',delimiter,'?[ ]*)'];
end
num_xpr = '([+-]?(\d+(\.\d*)?)|(\.\d+))';
sen_xpr = '([EeDd](\+|-)\d{1,3})?'; % optional scientific
num_xpr = [ num_xpr, sen_xpr ];
nl_xpr = '((\r\n)|\n)';
row_xpr = cat( 2, '(^|', nl_xpr, ')[ ]*(' ...
, num_xpr, del_xpr, ')*' ...
, num_xpr, trl_xpr, '(?=' ...
, nl_xpr,'|$)' );
blk_xpr = ['(',row_xpr,')+'];
blocks = regexp( buffer, blk_xpr, 'match' );
is_long = cellfun( @(str) length(str)>=block_size, blocks );
blocks(not(is_long)) = [];
out = cell( 1, length( blocks ) );
for jj = 1 : length( blocks )
out{jj} = str2num( blocks{jj} );
end
end
More Answers (1)
Chad Greene
on 24 Nov 2014
Use textscan.
fid = fopen('myfile.txt');
data = textscan(fid,'%f %f %f','headerlines',2);
frequency = data{1};
S1 = data{2};
S2 = data{3};
You'll need to verify that the number of headerlines is correct.
2 Comments
See Also
Categories
Find more on String Parsing 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!