Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Write Matlab binary MAT-files from Perl

by Anonymous Monk
on Jul 25, 2014 at 18:19 UTC ( [id://1095077]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,
I'm trying to write matlab binary MAT files from Perl.
Does anyone have a working routine/function that does that already?
I guess I'm not the first one trying to do this... but couldn't find a solution on this website or other pages.

I've been trying to follow this thread:
http://mathforum.org/kb/message.jspa?messageID=7738560
which basically works, but only for arrays that are shorter than 9 elements...
Arrays longer than that (in most cases) cannot be loaded correctly into matlab and produce the following error message:

> ??? Error using ==> load > Unable to read MAT-file C:\tools\mat4.mat > > File may be corrupt.

For example:
This works fine:
> mat4_write('foo', 1, 9, (1,2,3,4,5,6,7,8,9), *MAT);
And this does not (generates the error message above when trying to load the file into matlab):
> mat4_write('foo', 1, 10, (1,2,3,4,5,6,7,8,9,10), *MAT);

I saw the mat4_write routine (from the link above) uses the 'pack' function in this way:
print ($h pack ('l5Z*d*', $type, $m, $n, $imag, $len, $name, @a));

I also noticed that the mat file created is very close to the needed format, but not identical...
And by comparing the created file to a similar file created from matlab (with same data, for a reference) I saw they differ in 1 additional byte...

I have never used the 'pack' function, and the help from the link below is hard to follow:
http://perldoc.perl.org/functions/pack.html
I couldn't find a match there for the 'l5Z*d*' format the routine above uses...

Can someone please help me with that...?
Does anyone have a working conversion function?
Please help!

Thanks,
Koby

Replies are listed 'Best First'.
Re: Write Matlab binary MAT-files from Perl (which byte)
by tye (Sage) on Jul 25, 2014 at 20:02 UTC
    I saw they differ in 1 additional byte...

    And you don't want to share what that byte's value and offset were? Maybe even which file had the extra byte? That information might lead to you getting a much more detailed and/or useful response (at least faster).

    I couldn't find a match there for the 'l5Z*d*' format the routine above uses...

    That is just 'l' (lower-case L) to be used on 5 arguments, 'Z' to be used on one argument with '*' meaning to just take the length from the argument, and 'd' to be used on each of the remaining arguments ('*').

    - tye        

      Hi tye,
      Thanks!
      I have no problem sharing the byte's offset, I have nothing to hide :)
      It looks like it's the 9th byte (Byte 8 starting from 0).

      It has the value 0x0D and it is apparently an un-needed extra byte located only on the file created in the Perl script and not in the same vector saved directly from matlab.

      You can see a screenshot below of the comparisong in BeyondCompare:
      (I hope you can see the picture in the link below)

      http://srv2.jpg.co.il/2/53d2c3d4335d0.jpg

      Do you have any idea how to fix that...?

      Thanks,
      Koby

Re: Write Matlab binary MAT-files from Perl
by AppleFritter (Vicar) on Jul 25, 2014 at 18:28 UTC

    I have never used the 'pack' function, and the help from the link below is hard to follow:
    http://perldoc.perl.org/functions/pack.html
    I couldn't find a match there for the 'l5Z*d*' format the routine above uses...

    Take a look at perlpacktut.

    (No experience with Matlab, sorry. CPAN has no modules for writing MAT files either, apparently; maybe someone should create one.)

      FWIW, Data::MATFile does reading with unpack ... its clues at least :)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1095077]
Approved by AppleFritter
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-23 21:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found