Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^4: Write Matlab binary MAT-files from Perl (binmode)

by Anonymous Monk
on Jul 25, 2014 at 21:43 UTC ( [id://1095103]=note: print w/replies, xml ) Need Help??


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

Thanks tye!
I saw the link, and tried to read it.
I admit it was quite intimidating at first, but actually quite simple!
Thank you very much!
The script works now!
You've helped me a lot!

For others who might find this thread and need this MAT saving routine from Perl,
below you can see the modified function. Works perfectly now (with binmode activated)!

Thanks,
Koby

sub mat4_write { # Variable name. my $name = shift; # Number of rows. my $m = shift; # Number of columns. my $n = shift; # Matrix elements in column major layout, this is the native # Matlab storage layout for matrices. my @a = splice (@_, 0, $m * $n); # File handle. my $h = shift; # use binnary-mode: binmode($h); # Type flag. my $type = $mat4_type; # No imaginary part. my $imag = 0; # Length of variable name including terminating null character. my $len = length ($name) + 1; # Encode and print matrix. print ($h pack ('l5Z*d*', $type, $m, $n, $imag, $len, $name, @a)); }

Replies are listed 'Best First'.
Re^5: Write Matlab binary MAT-files from Perl (binmode)
by Anonymous Monk on Jul 25, 2014 at 22:37 UTC

    Thanks tye for your help!
    One last question, if you may.
    Now that if finally works, I'm trying to save also an array of strings instead of numbers (strings with the same length, as matlab requires) .
    Do you have any idea how should I change the routine above to write matrix with characters (text) in this binary MAT file?

    I guess I should change the pack format string: 'l5Z*d*'.
    From what you wrote at the beginning, I guess I should change the d (A double-precision float in native format) to some of the string/text options...
    I tried several string/text options (a,A,Z,b,h) but none of them worked...
    Some create zeros instead of characters, and some create illegal file that cannot be read by matlab...

    Do you have any idea how should I change the 'pack' format string...?
    Perhaps the format string is not enough...?

    Thanks,
    Koby

      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        

        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)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (3)
As of 2024-04-23 23:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found