3

I would like to call MATLAB from a software developed in C. I know how to call MATLAB, by using a system call using the standard syntax:

matlab -nojvm

My problem is that each time I spend a lot of time in opening a new session of MATLAB(when the requested processing ends, MATLAB closes, so I have to re-execute it when new data is ready for processing).

Is there any way to keep a MATLAB session always open (and use it as a "server"), and how to "inform" it that new data are available to be processed? This will avoid the pain of executing each time a new session of MATLAB, which is really time consuming.

Charles
  • 50,943
  • 13
  • 104
  • 142
  • 1
    This is called deaemonizing and can be done as described [here](http://www-theorie.physik.unizh.ch/~dpotter/howto/daemonize). – Bort Oct 28 '13 at 00:58
  • You only need to fully daemonize it (detach it from the shell) if the Matlab session/server has a different lifetime than the parent process. It's not clear whether that is needed from the original description. – wrdieter Oct 28 '13 at 02:29
  • 1
    you could also have a look at [this answer](http://stackoverflow.com/questions/12306333/matlab-command-from-bash-command-line-on-an-already-running-session/12307838#12307838) or maybe even [this one](http://stackoverflow.com/questions/18781803/call-a-function-by-an-external-application-without-opening-a-new-instance-of-mat) – Robert Seifert Oct 28 '13 at 08:23

2 Answers2

0

MATLAB has an input function that listens to stdin. Consider the following script that waits for your upstream C program to enter the filename of a chunk of data it has written and flushed to disk:

while true()
    % wait for upstream process to enter filename
    filename = input('', 's');
    % quit means we're done
    if strcmp(filename, 'quit')
        quit()
    end
    % run secondary processing as instructed
    process_chunk(filename);
end

The C program can simply write the name of a data file to stdout. After the last chunk has been written, it can even tell the MATLAB script to terminate by saying quit. The C program, in essence, could look like this:

for (int n = 0; n < N_CHUNK; n ++)
{
    // generate a filename for this chunk
    char filename[32];
    sprintf(filename, "chunk%d.txt", n);
    // do the actual work, generating a data file
    write_chunk(n, filename);
    // tell MATLAB to process that file
    printf("%s\n", filename);
}
// tell MATLAB we're done
printf("quit\n");

To make both work together, the C program has to "type" into the MATLAB process. If the C program is named foo and the script is in bar.m, you chain them together like this:

$ foo | matlab -nojvm -nodisplay -r bar

I just tested this on MATLAB R2013a (8.1.0.604), but I would not be surprised if input has been listening to stdin for a long time, and if this works with virtually any version of MATLAB on Linux.

s.bandara
  • 5,636
  • 1
  • 21
  • 36
0

Mathworks provides some documentation and sample code on how to call Matlab from C, C++, and Fortran in their Documentation Center. It looks like you can set Matlab variables and pass in arbitrary strings then read the results from other variables.

If you really do want to write to Matlab's stdin and stdout, probably the cleanest way would be to:

  1. Call pipe(2) twice to create a read/write pair of pipes to connect to Matlab's stdin and stdout.
  2. Call fork(2) to create a new child process.
  3. In the child process, use dup(2) to
    1. Copy the read end of one pipe over file descriptor 0 (stdin), and
    2. Copy the write end of the other pipe over file descriptor 1 (stdout).
  4. Have the child call execve(2) to invoke Matlab

The parent, can now write commands into the first pipe and they will be sent to Matlab. You may have to call flush after each command depending on how you open and write to the pipe. The second pipe, connected to Matlab's stdout, will capture the text output from Matlab.

I am not sure if Matlab sends anything to stderr. If so and you want to capture it, create a third pipe tied to stderr (file descriptor 2). Otherwise, it will go to your original program's stderr, which may or may not be what you want.

I have not used Matlab's C/C++ interface, but I would guess that using it is probably easier than trying to parse Matlab's output.

wrdieter
  • 2,836
  • 1
  • 21
  • 19