in reply to Re^5: Write Matlab binary MAT-files from Perl (binmode)
in thread Write Matlab binary MAT-files from Perl

For fixed-length formats (like 'd'), a '*' means "consume the remaining arguments". For formats that can have a length (like the ones you are trying to switch to), a '*' means "use the length of the next argument".

So you need to repeat the variable-length format specifications an appropriate number of times. Like:

'l5Z*'.('Z*'x@s)

- tye        

Replies are listed 'Best First'.
Re^7: Write Matlab binary MAT-files from Perl (repeat)
by Anonymous Monk on Jul 25, 2014 at 23:41 UTC

    Hi tye,
    I'm sorry, but it didn't work...

    Just to make sure I understood correctly:
    I changed the print line from this:
    print ($h pack ('l5Z*d*', $type, $m, $n, $imag, $len, $name, @a));
    to this:
    print ($h pack ('l5Z*'.('Z*'x@s), $type, $m, $n, $imag, $len, $name, @a));

    and from the main routine called:
    mat4_write ('foo', 3, 1, ('aaa','bbb','ccc'), *MAT);

    If you have another idea I would love to try it.
    If not, I will give it up and save string arrays as their ascii numbers representations and convert back to strings inside matlab...

    Thanks and good night,
    Koby

    p.s: Does Perl has a built-in function that converts a string to an array of its ascii numbers representation?
    i.e.: 'ABC' -> (65,66,67)

      What's your perl version? Try this perhaps:

      print $h pack 'l4 l/Z (Z*)*', $type, $m, $n, $imag, $name, @a;
      Re: ascii string to characters—that's simple: my @arr = unpack 'C*', $x;

        Re: ascii string to characters—that's simple: my @arr = unpack 'C*', $x;

        Not if its packed with 'Z*'.

        Plus: Your '(Z*)*' is no substitute for 'd*'. Remember the OP said that d* worked for arrays of less than 10 values.

        Tye called it above with binmode; your addition is unnecessary and wrong.


        With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

      Forgot to mention that the created file was loaded correctly to Matlab, but the matrix has 3 zeros instead of the 3 desired strings.

      Thanks,
      Koby

        In case this helps, here's the binary comparison between the file the Perl script created (on the left) and the file by matlab as a reference (on the right):
        http://srv2.jpg.co.il/8/53d2ed38850fb.jpg

        I'm going to sleep now,
        it's really late here :)
        Will return in the morning.

        Thanks tye for all your help!
        Koby