Reading PC MATLAB *.MAT files into Mathematica
- To: mathgroup at yoda.physics.unc.edu
- Subject: Reading PC MATLAB *.MAT files into Mathematica
- From: mek at guinan.psu.edu (Mark E. Kotanchek)
- Date: Tue, 14 Sep 93 21:09:24 -0400
Hello, 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 -1.15000000000000e+01 6.50000035762787e-01 1.63523000015020e+04 -1.21000005006790e+01 -1.34000005722046e+01 1.07500004768372e+00 1.63698500018120e+04 -1.77500004768372e+01 -2.08000011444092e+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 well. 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. Sigh..... In[333]:= SetDirectory["~/Submissions/1992_Aug_28_Data/range"] 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 ] Close[stream] Out[333]= /Home/guinan/mek/Submissions/1992_Aug_28_Data/range Out[335]= InputStream[XTV.MAT, 40] Out[336]= {0, 1125, 4, 0, 4} Out[343]= Double ReadBinary::nonnormal: 53311 Warning: Binary value ---------------- 4503599627370496 was not in the normalized floating point format; value may not be correct. Out[344]= 53311 {----------------} 4503599627370496 Out[345]= XTV.MAT 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. Thanks, Mark. --- Mark Kotanchek Guidance & Control Dept - 363 ASB Applied Research Lab/Penn State P.O. Box 30 State College, PA 16804 e-mail: mek at guinan.psu.edu (NeXTmail) TEL: (814)863-0682 FAX: (814)863-7843