1
$\begingroup$

I have a binary file 00010005.dat. I want to normalize the matrix so I use the following code:

normalize[file_] := 
   Module[{matfile = OpenRead[file, BinaryFormat -> True], data, Mat}, 
     SetStreamPosition[matfile, 0];
     data =  Partition[BinaryReadList[matfile, "Real32", ByteOrdering -> 1], 1297];
     Close[matfile]; 
     Mat = 
       Join[
         data[[All, ;; 45]], 
         Transpose[Standardize /@ Transpose[data[[All, 46 ;; 47]]]], 
         data[[All, 48 ;;]], 
         2]; 
     BinaryWrite[file, Flatten @ Mat, "Real32", ByteOrdering -> 1];];

the size of the file is:

FileByteCount["00010005.dat"]/2^10.
(* 891.688 *)

Then I normalize the file:

Do[normalize["00010005.dat"];, 5]

But this will change the size of the file.

 FileByteCount["00010005.dat"]/2^10.
 (* 14267. *)

Very strange. And using Streams I get:

enter image description here

But in normalize I already close the file actually. (Maybe I use the Close wrongly?)

So what's my problem and how do I fix it?

$\endgroup$
  • $\begingroup$ Why do you run the normalize code five times? $\endgroup$ – m_goldberg Feb 21 '17 at 19:23
  • 4
    $\begingroup$ You close the stream after reading but not after writing. $\endgroup$ – Simon Woods Feb 21 '17 at 19:42
  • $\begingroup$ Its good practice to do a explicit OpenWrite, that way you will get a nice error message if the file is already open because you forgot to close it. $\endgroup$ – george2079 Feb 21 '17 at 21:13
  • $\begingroup$ @SimonWoods you are right ,Thank you! $\endgroup$ – partida Feb 22 '17 at 11:30