views:

602

answers:

2

I have to test some "random numbers" generatied by a program in matlab, with diehard. it accept only 32 bit binary file, while if i save my data in matlab, they are saved in binary file with double precision (so 2*64=128 bit binary file). how can i take a 32 bit binary file in matlab, working on a 64 bit system?

+5  A: 

If you want to read/write data to a binary file in a specific format, you should use the functions FREAD/FWRITE. For example, this will write 100 random values to a file as 32-bit floats:

A = rand(1,100);
fid = fopen('temp.dat','wb');
fwrite(fid,A,'float32');
fclose(fid);

For more info about file IO in MATLAB, you can also check out these other related SO posts: here and here.

gnovice
+2  A: 

Further to gnovice's solution, you may wish to generate the random numbers as 'single', like so:

rand(1, 100, 'single')
Edric
This is good to remember if you are dealing with very large matrices, since it will take up less space than the default double precision that RAND would use. However, you still have to specify the precision for FWRITE to use. If it isn't specified, FWRITE will default to 'uint8' precision (for some reason, it doesn't just choose the precision of the variable you pass it).
gnovice