Write/read binary files

11 views (last 30 days)
Hoan Nguyen
Hoan Nguyen on 31 Mar 2020
Answered: per isakson on 31 Mar 2020
Hi,
Not sure where the bugs are or what I did wrong. I saved arrays of complex numbers in a Matlab binary file and then read out the numbers from the saved file. The values don't match. Saved values are e^-6 and read-out values are e^389. Codes are below.
%write codes
fido = fopen(filename_out,'w','ieee-be');
tmpdatR(:,:) = real((X));
tmpdatI(:,:) = imag((X));
tmpdat = [tmpdatR,tmpdatI];
e=fwrite(fido,tmpdat,'double'); %fwrite is in a loop
fclose(fido)
%read codes
fido=fopen(filename,'r','ieee-be');
XCtemp=fread(fido,inf,'double');
fclose(fido)

Answers (1)

per isakson
per isakson on 31 Mar 2020
This script
%%
ffs = 'test.bin';
X = 1+2i;
%% write codes
fid = fopen(ffs,'w','ieee-be');
tmpdatR = real((X));
tmpdatI = imag((X));
tmpdat = [tmpdatR,tmpdatI];
e=fwrite(fid,tmpdat,'double'); %fwrite is in a loop
fclose(fid);
%% read codes
fid=fopen(ffs,'r','ieee-be');
XCtemp=fread(fid,inf,'double');
fclose(fid);
%%
tmpdat, XCtemp
outputs
tmpdat =
1 2
XCtemp =
1
2
I'm not sure what's going wrong with your code

Categories

Find more on Data Import and Analysis 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!