freadcomplex and fwritecomplex
freadcomplex and fwritecomplex are mex routines that read and write an interleaved complex data file for R2018a or later. It first reads the file as real with twice the first dimension using the MATLAB fread function, then internally converts this variable into an interleaved complex variable with the original requested size. The internal conversion from real to complex is a straight conversion without any extra data copy. For writing, it creates a temporary pseudo shared data real variable and then calls the MATLAB fwrite function. That is, no extra data copies are involved in the reading and writing.
Since these mex routines use the MATLAB fread and fwrite functions, the syntax is the same as these functions:
A = freadcomplex(fileID)
A = freadcomplex(fileID,sizeA)
A = freadcomplex(fileID,sizeA,precision)
A = freadcomplex(fileID,sizeA,precision,skip)
A = freadcomplex(fileID,sizeA,precision,skip,machinefmt)
[A,count] = freadcomplex(___)
fwritecomplex(fileID,A)
fwritecomplex(fileID,A,precision)
fwritecomplex(fileID,A,precision,skip)
fwritecomplex(fileID,A,precision,skip,machinefmt)
count = fwritecomplex(___)
E.g.,
>> x = reshape(1:16,4,4)
x =
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
>> fid = fopen('test.dat','wb');
>> fwrite(fid,x,'double');
>> fclose(fid);
>> fid = fopen('test.dat','rb');
>> freadcomplex(fid,'*double') % read as column
ans =
1.0000 + 2.0000i
3.0000 + 4.0000i
5.0000 + 6.0000i
7.0000 + 8.0000i
9.0000 +10.0000i
11.0000 +12.0000i
13.0000 +14.0000i
15.0000 +16.0000i
>> frewind(fid);
>> freadcomplex(fid,[2 4],'*double') % read as halve first dimension
ans =
1.0000 + 2.0000i 5.0000 + 6.0000i 9.0000 +10.0000i 13.0000 +14.0000i
3.0000 + 4.0000i 7.0000 + 8.0000i 11.0000 +12.0000i 15.0000 +16.0000i
>> frewind(fid);
>> freadcomplex(fid,[4 2],'*double') % read as halve second dimension
ans =
1.0000 + 2.0000i 9.0000 +10.0000i
3.0000 + 4.0000i 11.0000 +12.0000i
5.0000 + 6.0000i 13.0000 +14.0000i
7.0000 + 8.0000i 15.0000 +16.0000i
>> fclose(fid);
Cite As
James Tursa (2024). freadcomplex and fwritecomplex (https://www.mathworks.com/matlabcentral/fileexchange/77530-freadcomplex-and-fwritecomplex), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Platform Compatibility
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.