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.