MathGroup Archive 1993

[Date Index] [Thread Index] [Author Index]

Search the Archive

Reading PC MATLAB *.MAT files into Mathematica

  • To: mathgroup at
  • Subject: Reading PC MATLAB *.MAT files into Mathematica
  • From: mek at (Mark E. Kotanchek)
  • Date: Tue, 14 Sep 93 21:09:24 -0400


I've got some large MATLAB files which have been transferred to my NeXTstation and  
which are too large to fit on my Powerbook's hard disk. I need to process these files  
and get them down to a more reasonable size. Alas, I seem to be unable to read them  
into Mathematica. If I generate test MATLAB files on my Mac I seem to be able to read  
them in correctly; however, the PC-derived files are not handled correctly. The code is  
listed below. The file in question is relatively small (35 KB0 so I transferred it over  
to the Mac and loaded it into MATLAB without any problems. I subsequently resaved the  
file as ASCII and transferred it back to the NeXT:

   2.50000000000000e-01   1.63340000000000e+04  -6.50000000000000e+00   
   6.50000035762787e-01   1.63523000015020e+04  -1.21000005006790e+01   
   1.07500004768372e+00   1.63698500018120e+04  -1.77500004768372e+01   

as you can see, the results differ significantly from the Mathematica results.  
Unfortunately, the "real" data files are on the order of 6 MB each; hence, I don't have  
the option of transferring them to the Mac since I'll need to have multiple files open  
simultaneously and I don't have that much disk space.

I guess I can write a C program, but I was hoping to use Mma so that I could look at  
the data graphically so that I could get a warm fuzzy feeling that things were working  

I've tried various permutations on the script below without any good results. I'm  
getting pretty frustrated at the moment...and I haven't even gotten into any of the  
problems associated with making the thing general. Right now it appears that I have to  
specify the ByteOrder and dataFormat inside the ReadBinary[] calls to get the correct  
data type.


   filename = "XTV.MAT";
   stream = OpenReadBinary[filename]
   {MOPT,Mrows,Ncols,imagFlag,nameLength} = 

   		ReadBinary[stream, {Int32,Int32,Int32,Int32,Int32},
   		ByteOrder -> LeastSignificantByteFirst ]
   name = "";
   Do[name = name<>Read[stream,Character],{nameLength}]
   (* Read[stream,Character]; *)

   machine = Floor[MOPT/1000.];
   dataType = Floor[(MOPT - 1000 machine)/10];
   matrixType = Floor[MOPT - 1000 machine - 10 dataType];
   dataFormat = Switch[dataType,
   	0, Double, 

   	1, Single,
   	2, SignedInt32,
   	3, SignedInt16,
   	4, Int16,
   	5, Int8]
   data = ReadBinary[stream,{Double}, 

   			ByteOrder -> LeastSignificantByteFirst ]

   InputStream[XTV.MAT, 40]

   {0, 1125, 4, 0, 4}



      Warning: Binary value ----------------
        was not in the normalized floating point format;  value may
        not be correct.



Thanks for any help you can give me. At the moment I'm amazed at all those people who  
advocate using Mma to teach programming! Given the inability of the modern  
(non-technical) college student to comprehend things like square roots, I feel sorry  
for those trying to get this monster to dance.


Mark Kotanchek
Guidance & Control Dept - 363 ASB
Applied Research Lab/Penn State
P.O. Box 30
State College, PA 16804

e-mail:	mek at (NeXTmail)
TEL:	(814)863-0682
FAX:	(814)863-7843

  • Prev by Date: unknown warning message
  • Next by Date: Re: Failure to recognize "1"
  • Previous by thread: unknown warning message
  • Next by thread: Mathematica for elementary and junior high students