How do I read a file with complex data (real + imaginary) into a vector?
1 view (last 30 days)
Show older comments
I have a file with 100 rows of complex data with variable column size. How do I read it into a vector of 70: [] ?
If row 1 is bigger than row 2, then I like the row 1 padded by default (like in dlmread).
4 Comments
Answers (1)
per isakson
on 12 Dec 2019
Edited: per isakson
on 12 Dec 2019
What problem do you encounter? With R2018b dlmread() reads your small data set nicely. Padding is done with 0+0i
>> C = dlmread( 'data.txt' )
C =
Columns 1 through 3
1.868 - 0.52591i -0.4178 + 1.0316i 0.5408 - 0.74036i
0.33257 - 0.20658i -0.56193 - 1.6297i -0.3833 + 0.555i
0.6894 + 0.60439i 1.2882 + 0.12116i 1.0911 + 0.40315i
0.36288 + 1.0868i -0.7524 + 0.72005i -1.8897 + 0.25002i
Columns 4 through 6
-0.024144 + 1.6425i -0.34343 + 0.96012i 0 + 0i
0.58934 - 0.053148i 0.18756 - 0.53192i -1.0676 + 0.88013i
-0.63711 - 0.91396i -0.70589 + 0.54294i -0.28131 + 0.41173i
1.3212 + 1.9887i 0 + 0i 0 + 0i
>>
where data.txt contains the data of your comment,
"a vector of 70: [] ?"
D = reshape( C, 6,[] ); % I replaced 70 by 6 to match this small data set
"If row 1 is bigger than row 2, then I like the row 1 padded"
You don't mean to pad the "bigger" row?
In response to comments
This script does for the large file what dlmread() did for the small file
%%
fid = fopen( 'd:\m\cssm\BerNoisySignal.dat', 'rt' );
cac = cell(0,1);
while not( feof( fid ) )
chr = fgetl( fid );
cac{end+1,1} = textscan( chr, '%f', 'Delimiter','\t' );
disp( length(cac) ) % show progress
end
fclose( fid );
%%
wid = max( cellfun( @(c) numel(c{1}), cac ) );
C = complex( zeros( length(cac), wid ) );
%%
for jj = 1 : length(cac)
num = reshape( cac{jj}{1}, 1,[] );
C(jj,1:length(num)) = num;
end
%%
figure,imagesc(real(C))
outputs
and
>> C(30:35,1e5:1e5+2)
ans =
0.473 + 0.24055i -0.10531 + 0.21882i 0.60661 + 0.44345i
0.30427 + 0.43949i 0.75655 - 1.0476i 0.20642 + 0.40645i
0.82177 - 0.10199i 0.17799 + 0.10666i 0.44184 + 0.88519i
0.29776 - 0.44033i -0.25417 + 0.0089458i -0.048546 + 0.9869i
0.96475 + 0.48504i -0.58236 - 0.7256i -0.019985 - 0.6513i
1.1374 - 0.1369i 0.19644 - 0.021786i -1.0417 - 0.55171
>> C(28:33,wid-2:wid)
ans =
0 + 0i 0 + 0i 0 + 0i
0 + 0i 0 + 0i 0 + 0i
-0.22192 + 0.68818i -0.91552 - 0.17804i -0.2485 + 0.90526i
0 + 0i 0 + 0i 0 + 0i
0 + 0i 0 + 0i 0 + 0i
0 + 0i 0 + 0i 0 + 0i
and
>> D = reshape( C, 70,[] );
>> whos C D
Name Size Bytes Class Attributes
C 35x155000 86800000 double complex
D 70x77500 86800000 double complex
11 Comments
See Also
Categories
Find more on Logical 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!