From sources-request at octave dot org Thu Apr 21 09:14:39 2005 Subject: Matlab compressed files From: Philipp =?iso-8859-1?q?G=FChring?= To: sources at octave dot org Date: Thu, 21 Apr 2005 03:39:02 -0500 --Boundary-00=_rY2ZCDjg0xTH0Hf Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Hi, Octave cannot handle Matlab compressed files at the moment. According the the current fileformat specification: http://www.mathworks.com/access/helpdesk/help/pdf_doc/matlab/matfile_format= =2Epdf compressed Tags have miCOMPRESSED (15), and it is simply zlib compressed. I have written a decompressor in Perl, that does the job cleanly. Usage: perl matlab_decompress.pl I tried to write decompression support into Octave itself, but I didn=B4t k= new=20 how to do the memory handling, since the fileformat does not say, how large= =20 the decompressed memory is, but the zlib library needs a large enough buffe= r. The decompression is really simple, you just need the uncompress function f= rom=20 zlib: my $dest =3D uncompress($buffer); Regards, =2D-=20 Philipp G=FChring --Boundary-00=_rY2ZCDjg0xTH0Hf Content-Type: application/x-perl; name="matlab_decompress.pl" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="matlab_decompress.pl" #!/usr/bin/perl -w use Compress::Zlib; my $filename=$ARGV[0]; $filename=~s/\.mat$/_uncompressed.mat/; open OUT,">$filename"; sub readmatlab($) { my $d=$_[0]; my $f=undef; if(substr($d,0,4) eq "MATL") { print "Detected Matlab file\n"; } else { return undef}; if(substr($d,124,2) eq "\x00\x01") { print "Version: 1.0\n"; } if(substr($d,126,2) eq "IM") { print "Big Endian\n"; $f="V"; } elsif(substr($d,126,2) eq "MI") { print "Little Endian\n"; } else { print "Dateiformat nicht erkennbar!\n"; } print OUT substr($d,0,128); my $pos=128; my $todo=1; while($pos< length($d)) { my $DataType=unpack($f,substr($d,$pos,4)); my $NumberBytes=unpack($f,substr($d,$pos+4,4)); my $buffer=substr($d,$pos+8,$NumberBytes); print "Paket mit Format $DataType mit $NumberBytes Bytes.\n"; if($DataType == 15) { print "Compressed Tag detected. Ungzipping ...\n"; my $dest = uncompress($buffer); # my $dest = Compress::Zlib::memGunzip($buffer); print "---".$dest."---\n"; print "---".$buffer."---\n"; print OUT $dest; } else { print OUT substr($d,$pos,8+$NumberBytes); } $pos+=$NumberBytes+8; } } sub readmatlabfile($) { print "Lese $_[0]\n"; if(open(IN,"<$_[0]")) { my $old=$/; undef $/; my $content=; close IN; $/=$old; return readmatlab($content); } return undef; } readmatlabfile($ARGV[0]); close OUT; --Boundary-00=_rY2ZCDjg0xTH0Hf--