Matlab interface

1,003 views
Skip to first unread message

gerald...@gmail.com

unread,
Aug 26, 2014, 5:39:45 AM8/26/14
to golan...@googlegroups.com
Hi,

is there a way to call a golang function within a Matlab environment. I know what you can call C/C++ functions within Matlab...

Thanks,
Gerald

Maxim Khitrov

unread,
Aug 26, 2014, 8:15:52 AM8/26/14
to gerald...@gmail.com, golang-nuts
You can call into MATLAB from Go via cgo, but not the other way
around. Here's some relevant documentation:

http://www.mathworks.com/help/matlab/calling-external-functions.html
http://www.mathworks.com/help/matlab/programming-interfaces-for-c-c-fortran-com.html

- Max

Gerald Stanje

unread,
Aug 26, 2014, 8:40:44 AM8/26/14
to Maxim Khitrov, golang-nuts
Yes, matlab supports c/c++ function calls...
But you could call a golang function from c?

Sent from my iPhone

Maxim Khitrov

unread,
Aug 26, 2014, 9:34:12 AM8/26/14
to Gerald Stanje, golang-nuts
Not unless something has changed recently with the runtime. You can
make Go -> C -> Go calls, but not C -> Go. In other words, the Go
runtime needs to be the first thing to run. Of course, you can create
a separate Go executable and communicate with it through files,
network, or any other IPC mechanism.

gerald...@gmail.com

unread,
Aug 26, 2014, 12:15:48 PM8/26/14
to golan...@googlegroups.com, gerald...@gmail.com
yes...runtime does gc and other stuff...

i see that matlab can call java code using JMI....could that be applied to golang too?

Maxim Khitrov

unread,
Aug 26, 2014, 12:21:23 PM8/26/14
to Gerald Stan, golang-nuts
If the developers decide to implement a way of initializing the
runtime from C, then it would be possible. I don't think there are any
plans to do that at the moment.
> --
> You received this message because you are subscribed to the Google Groups
> "golang-nuts" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to golang-nuts...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

gerald...@gmail.com

unread,
Sep 2, 2014, 1:11:06 PM9/2/14
to golan...@googlegroups.com, gerald...@gmail.com
what IPC technique would you choose between matlab and golang if speed matters?
sockets?
from what i see matlab can do sockets...

another way is: call the executable from matlab...i guess thats rather slow?

Maxim Khitrov

unread,
Sep 2, 2014, 1:33:40 PM9/2/14
to Gerald Stan, golang-nuts
It depends entirely on what you're trying to do and whether you mean
speed as in latency or throughput. You could use memory maps, as in
the following example:

http://www.mathworks.com/help/matlab/import_export/share-memory-between-applications.html

Network is another option, calling an executable may also work. For
anything that you can't do directly in MATLAB, you can compile mex
functions from C or write your own Java classes.

Gerald Stan

unread,
Sep 2, 2014, 2:57:50 PM9/2/14
to Maxim Khitrov, golang-nuts
thats what I already did...
calling a simple hello_world executable takes around 400ms...
so looking for another option...

im not sure if golang can do memory maps!?
so sockets would be the best option i guess...

Maxim Khitrov:



I would start with something simple, like using the system function to
run your Go binary and passing data via stdin/stdout or files, measure
the performance, and then move on to more complicated methods, if
needed.


2014-09-02 20:16 GMT+02:00 Maxim Khitrov <m...@mxcrypt.com>:
I would start with something simple, like using the system function to
run your Go binary and passing data via stdin/stdout or files, measure
the performance, and then move on to more complicated methods, if
needed.

On Tue, Sep 2, 2014 at 1:38 PM, Gerald Stan <gerald...@gmail.com> wrote:
> i do some parsing with golang...matlab sends the text to be parsed to
> golang, go does the parsing and needs to send it back to matlab...
> i need an option with low latency...
>
> yeah, but i cant compile my go source with mex...

Maxim Khitrov

unread,
Sep 2, 2014, 3:18:17 PM9/2/14
to Gerald Stan, golang-nuts
I just tested this by running "tic; system('hello'); toc" and got ~50ms on Windows and ~5ms on Linux with MATLAB R2014a. Not sure why you're getting 400ms. Either way, if you need even lower latency, then the Go process has to be running already when MATLAB submits the request.

For memory maps see http://golang.org/pkg/syscall/#Mmap. If you are on *nix, mkfifo might be another option.

gerald...@gmail.com

unread,
Sep 2, 2014, 4:44:12 PM9/2/14
to golan...@googlegroups.com, gerald...@gmail.com
here my test:
golang:

matlab:
clear;
clc;

tic;
[status, result] =  system(['goapp.exe -s hello']);
toc;

gerald...@gmail.com

unread,
Sep 3, 2014, 3:38:14 AM9/3/14
to golan...@googlegroups.com, gerald...@gmail.com
ok, i some more measurements now:
- Elapsed time call system('dir') from matlab is ~0.199997 seconds.

- Elapsed time call my app and pass a parameter is ~0.752352 seconds: ... does that sound reasonable on windows x32?
tic
[status, result] = system('goapp.exe -name=test');
result
toc

here the goapp:

Maxim Khitrov

unread,
Sep 3, 2014, 8:47:59 AM9/3/14
to Gerald Stan, golang-nuts
I ran your goapp.exe 100 times using the same MATLAB commands on Windows 7 64-bit and got a mean time of 0.062 seconds. I don't know what hardware you're using, but 0.752 seems a bit slow. Do you have some antivirus that might be doing a scan of the executable every time you run it? I don't have any other suggestions and at this point the issue is related more to MATLAB than to Go.

gerald...@gmail.com

unread,
Sep 3, 2014, 2:13:14 PM9/3/14
to golan...@googlegroups.com
I didnt see that the executable was on a network drive. ;) ...getting similar times now.
Message has been deleted

gerald...@gmail.com

unread,
Sep 3, 2014, 2:18:28 PM9/3/14
to golan...@googlegroups.com

gerald...@gmail.com

unread,
Sep 3, 2014, 6:03:52 PM9/3/14
to golan...@googlegroups.com, gerald...@gmail.com
seems shared libraries are in plan and could be used in non go programs a a plugin:

brainman

unread,
Sep 3, 2014, 9:35:24 PM9/3/14
to golan...@googlegroups.com, gerald...@gmail.com
On Thursday, 4 September 2014 04:13:14 UTC+10, gerald...@gmail.com wrote:
I didnt see that the executable was on a network drive. ;) ...getting similar times now.

Also remember that Windows must search your PATH for the executable. 

Alex
Reply all
Reply to author
Forward
0 new messages