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

..is it possible to produce a binary output file from Perl code to be read into MATLAB? Ideally the output file will conform to .m requirements so as to be easily read by a MATLAB script
  • Comment on outputting binary file for MATLAB (.m) files

Replies are listed 'Best First'.
Re: outputting binary file for MATLAB (.m) files
by dHarry (Abbot) on Nov 23, 2009 at 12:34 UTC

    Do you mean the MAT-file format? Yes that's possible. However there are several versions/formats around.

    Copied from the docs:

    Level 5 MAT-files are made up of a 128-byte header followed by one or more data elements. Each data element is composed of an 8-byte tag followed by the data in the element. The tag specifies the number of bytes in the data element and how these bytes should be interpreted; that is, should the bytes be read as 16-bit values, 32-bit values, floating-point values or some other data type.

    You will have to construct the files yourself using pack as suggested above.

    See matfile_format.pdf for a good description of the different formats.

    Cheers,

    Harry

    I'm so happy I don't have to work with MATLAB, IDL and other $%^$^$[!#~) languages anymore:)

Re: outputting binary file for MATLAB (.m) files
by moritz (Cardinal) on Nov 23, 2009 at 12:10 UTC
    My Matlab-fu is a bit weak, but I seem to recalls that binary matlab files are usually created from a text file.

    So it might be easiest to write the text file in your perl script, and let matlab convert it to binary.

    But it really depends on what you want to do: if you just want to transport data from perl to matlab code, you can write either CSV files or binary files with pack, both can be read from matlab.

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: outputting binary file for MATLAB (.m) files
by keszler (Priest) on Nov 23, 2009 at 11:36 UTC
    Assuming that you know the .m requirements (I don't), the pack function can transform perl data into the necessary binary format.