How do I read a file with complex data (real + imaginary) into a vector?

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

data.txt contains one huge row of tab-delimited complex data. Is that what you intended?
It should look like this.
1.868-0.52591i -0.4178+1.0316i 0.5408-0.74036i -0.024144+1.6425i -0.34343+0.96012i
0.33257-0.20658i -0.56193-1.6297i -0.3833+0.555i 0.58934-0.053148i 0.18756-0.53192i -1.0676+0.88013i
0.6894+0.60439i 1.2882+0.12116i 1.0911+0.40315i -0.63711-0.91396i -0.70589+0.54294i -0.28131+0.41173i
0.36288+1.0868i -0.7524+0.72005i -1.8897+0.25002i 1.3212+1.9887i
I have put the data on github, please have a look. Thanks.Data
github says: This repository is empty.

Sign in to comment.

Answers (1)

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

On tha data import tool it shows [70:15300] as my dataset. However, when I use dlmread, i.e., BerNoisySignal = dlmread('BerNoisySignal.dat');
it concatenates all the data and gives me a [8922000:1 ] vector.
My data is at Data. Thanks
I don't trust the data import tool. It tries to hard to be smart. Furthermore, I fail to interpret your screen-clips.
The file, data.txt, of your question contains two newline character. LF, at the end of the file.
>> C = dlmread( 'data.txt' );
>> whos C
Name Size Bytes Class Attributes
C 1x10000 160000 double complex
"My data is at Data. Thanks"
github still says. This repository is empty.
Could the reason be related to permissions?
I failed to use the link, New Data. The page, https://bitbucket.org/amohideen/data.git, is blank from my point of view. How am I supposed to download the data?
Can you git clone it?
git clone https://bitbucket.org/amohideen/data.git
or if you go Here Link you can download it (tested, sorry about the earlier blunder)
"or if you go Here Link you can download" that suits me better.
I've added a script to my answer.
Thanks. I will check it update you.

Sign in to comment.

Products

Release

R2018a

Tags

Asked:

on 11 Dec 2019

Commented:

on 17 Dec 2019

Community Treasure Hunt

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

Start Hunting!