caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
From: Jan Kybic <kybic@fel.cvut.cz>
To: caml-list@yquem.inria.fr
Subject: Ocaml <-> Matlab interface
Date: Wed, 04 Feb 2009 13:41:41 +0100	[thread overview]
Message-ID: <87myd2nzlm.fsf@fel.cvut.cz> (raw)

Good afternoon,
                I was using Ocamlmex (by Maurice Bremond)
for interfacing between Matlab and Ocaml. This interface is very
elegant, nice to work with and very complete. Unfortunately, I was
getting quite frequent "segmentation faults" from Matlab when trying
to pass matrices there and back. Probably some issues with memory
management between Ocaml and Matlab. I am posting below the relevant excerpts
from my code in case somebody can spot the problem.

Anyway, I have created an alternative Ocaml<->Matlab interface. Matlab
and Ocaml run as separate processes and communicate using Unix named
pipes (created using mkfifo). This is slower and less complete than
Ocamlmex solution but it is nevertheless usable and also quite stable
(no segfaults). The code is very 'alpha' at the moment but if somebody
is interested in using or improving it, just send me an email. I plan
to release the source code when it matures a little.

Yours,

Jan Kybic


-- 
-------------------------------------------------------------------------
Jan Kybic <kybic@fel.cvut.cz>                       tel. +420 2 2435 5721
http://cmp.felk.cvut.cz/~kybic                      ICQ 200569450

To get an array from Matlab:

  let speed_f = match Mex.mxGetData speedm with
        | Mex.FLOAT32 s -> (
            try
              array2_of_genarray s
            with Invalid_argument _ ->
              failwith "Input is not a 2D array." )
        | _ ->  failwith "Input is not float32 (class single)." in

To pass an array to Matlab:

let mxArray_of_floatarray f =
  let n,m = (Array2.dim1 f, Array2.dim2 f) in
  let r = Mex.mxCreateNumericArray [| n ; m |] Mex.DOUBLE_CLASS Mex.REAL in
  let a = Array2.create float64 fortran_layout n m in
  for j = 1 to m do
   for i = 1 to n do
     a.{i,j} <- f.{i-1,j-1} ;
   done ;
 done ;
 Mex.mxSetData r (Mex.FLOAT64 (genarray_of_array2 a)) ;
 r


             reply	other threads:[~2009-02-04 12:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-02-04 12:41 Jan Kybic [this message]
2009-02-05 14:48 ` [Caml-list] " Maurice Bremond

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87myd2nzlm.fsf@fel.cvut.cz \
    --to=kybic@fel.cvut.cz \
    --cc=caml-list@yquem.inria.fr \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).