MATLAB Answers

Write/read binary files

5 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)

  0 Comments

Sign in to comment.

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

  0 Comments

Sign in to comment.

Sign in to answer this question.


Translated by