Is it possible to use FREAD and FWRITE to read and write complex binary files?

68 views (last 30 days)
I would like to use FREAD and FWRITE to read and write complex binary files.
I would like to know what precision I would need to use in order to read or write a complex binary data file.
I would also like to write COMPLEX numbers to a binary file to be read into another application.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
FREAD and FWRITE both support the native ANSI C data types. There are no ANSI C data types that correspond to complex values. Hence, there is no precision for complex numbers.
As a workaround you could do something like the following and store areal and aimag in separate parts of your binary data file.
areal = real(a);
aimag = imag(a);
You also need to know the precision of your file before you can use FREAD, otherwise you will get incorrect results.
The following is an example of how to read and write complex data into a binary file from MATLAB.
z=complex(rand(5),rand(5)); % define random complex data
disp('The following are the REAL components:')
a=real(z) % split the real component into one variable called a
disp('The following are the IMAGINARY components:')
b=imag(z) % split the imaginary component into one variable called b
[r,c]=size(z); % find the size of original complex matrix
z1=[];
for cc=1:c
z1=[z1 a(:,cc) b(:,cc)]; % generate "standard" format for complex numbers [real imag, real imag,...]
end
disp(sprintf(['The following is 1 column of REAL components and then next \n' ...
'to it on the right is 1 column of IMAGINARY components (and then repeated):']))
z1
fid=fopen('testdata1','w'); % open a file for WRITING with the name testdata1
fwrite(fid,z1,'double'); % write the variable z1 in that file
fclose(fid);
disp('The following is the same as z1 (c1=z1) AS READ FROM THE BINARY FILE')
fid=fopen('testdata1','r'); % open a file for READING with the name testdata1
c1=fread(fid,[r,2*c],'double') % read the contents of testdata1 into variable c1 (in this case size=5x10)
fclose(fid);
Here is another way to output the data.
disp(sprintf(['The following is 5 columns of REAL components and then next \n' ...
'to it on the right are 5 columns of IMAGINARY components:']))
z2=[a b]
fid=fopen('testdata2','w'); % open a file for WRITTING with the name testdata2
fwrite(fid,z2,'double'); % write the variable z2 in that file
fclose(fid);
disp('The following is the same as z1 (c2=z1) AS READ FROM THE BINARY FILE')
fid=fopen('testdata2','r'); % open a file for READING with the name testdata2
c2=fread(fid,[r,2*c],'double') % read the contents of testdata1 into variable c2 (in this case size=5x10)
fclose(fid);
  4 Comments
Michael
Michael on 22 Aug 2022
That's what I though. I just ended up interleaving the real and complex parts manually for my read and write operations.
Michael
Michael on 23 Aug 2022
It looks like the interleaving is suggested by the Export Binary Data with Low-Level I/O documentation under the Write and Read Complex Numbers section. The documentation on fwrite should probably be updated to clarify with and example how complex numbers supported through interleaving or writing all real components followed by all imaginary components (as discussed in the Export Binary Data documentation).

Sign in to comment.

More Answers (0)

Tags

No tags entered yet.

Products

Community Treasure Hunt

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

Start Hunting!