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

 채택된 답변

per isakson
per isakson 2014년 11월 24일
편집: per isakson 2014년 11월 24일

1 개 추천

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
&nbsp
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

추가 답변 (1개)

Chad Greene
Chad Greene 2014년 11월 24일

1 개 추천

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

yes, but I need to load 4 fields in the same file. The file was exported from CST.
Your answer only build a field but not the three fields.
the format is like that:
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
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
Frequency / GHz S1,2/abs,linear S1,2/arg,degrees
----------------------------------------------------------------------------------------------------------------
0.5 0.97077229 314.9951
0.5055 0.970726 314.50112
0.511 0.97067948 314.00717
0.5165 0.97063276 313.51325
0.522 0.97058584 313.01937
Frequency / GHz S2,2/abs,linear S2,2/arg,degrees
----------------------------------------------------------------------------------------------------------------
0.5 0.23118246 275.34126
0.5055 0.23134189 274.45027
0.511 0.23150193 273.55911
0.5165 0.23166255 272.6678
0.522 0.23182371 271.77636
0.5275 0.2319854 270.8848
0.533 0.23214756 269.99316
0.5385 0.23231019 269.10144
Chad Greene
Chad Greene 2014년 11월 24일
Can you upload the text file so I can see what you mean?

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기

질문:

2014년 11월 24일

편집:

2014년 11월 24일

Community Treasure Hunt

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

Start Hunting!

Translated by