Ahmed Abobakr
Ahmed Abobakr

Reputation: 1666

Matlab Write an Integer to a binary file

I have an uint32 variable that needs to be stored as the first 4 bytes in a binary file. How can I do that using MATLAB?

What I have tried is converting the integer to an array of bytes, and then store them byte by byte in the binary file, but I could not do that.

Upvotes: 1

Views: 293

Answers (1)

rayryeng
rayryeng

Reputation: 104464

As @Ben just said, use fwrite. Supposing your uint32 variable is stored in A which is of type uint32, and supposing you want to write to a file called test.txt, simply do this:

fid = fopen('test.txt', 'w'); %// Open test.txt for writing
fwrite(fid, A, 'uint32'); %// Write this single uint32 number to file
fclose(fid); %// Close the file to register the changes

Upvotes: 2

Related Questions