Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Read from a pipe (stdin)

Subject: Read from a pipe (stdin)

From: Ed Scheinerman

Date: 30 Jun, 2008 11:33:02

Message: 1 of 4

The "data" I'd like to read into my matlab program is the
output of a C program. I'd like to be able to pipe the
output of the C program into matlab and have my .m file
process that data as it comes in.

I know I can write the output of the C program to disk and
then read that file in. But the output is likely to be very
large so I don't want to use that route.

Is this possible? If so, how?

Thanks.

Subject: Read from a pipe (stdin)

From: Ed Scheinerman

Date: 3 Jul, 2008 11:48:02

Message: 2 of 4

Here's a solution that I found to work well in a Unix environment.

(1) In the Unix shell, create a named pipe using the "mkfifo" command, for
example:
$ mkfifo MY_PIPE
In the directory where this was done a new file named MY_PIPE appears, but
it's not a file --- it's a named pipe.

(2) Redirect the output of your C program to that pipe, e.g.,
$ ./a.out > MY_PIPE

(3) Inside Matlab, use fopen('MY_PIPE','r') to read from the pipe exactly like
reading from an ordinary file.



"Ed Scheinerman" <ers@jhu.edw> wrote in message
<g4ag9e$h2a$1@fred.mathworks.com>...
> The "data" I'd like to read into my matlab program is the
> output of a C program. I'd like to be able to pipe the
> output of the C program into matlab and have my .m file
> process that data as it comes in.
>
> I know I can write the output of the C program to disk and
> then read that file in. But the output is likely to be very
> large so I don't want to use that route.
>
> Is this possible? If so, how?
>
> Thanks.
>

Subject: Read from a pipe (stdin)

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 3 Jul, 2008 16:51:41

Message: 3 of 4

In article <g4ie9i$srg$1@fred.mathworks.com>,
Ed Scheinerman <ers@jhu.edw> top-posted:

Please do not post your replies above the material you are commenting
on, as it makes it difficult to hold a conversation.

>"Ed Scheinerman" <ers@jhu.edw> wrote in message
><g4ag9e$h2a$1@fred.mathworks.com>...
>> The "data" I'd like to read into my matlab program is the
>> output of a C program. I'd like to be able to pipe the
>> output of the C program into matlab and have my .m file
>> process that data as it comes in.


>Here's a solution that I found to work well in a Unix environment.
>
>$ mkfifo MY_PIPE

>(2) Redirect the output of your C program to that pipe, e.g.,
>$ ./a.out > MY_PIPE

>(3) Inside Matlab, use fopen('MY_PIPE','r') to read from the pipe exactly like
>reading from an ordinary file.

Unfortunately this doesn't quite solve the original poster's question,
at least not -as stated-. The original poster requested that
the matlab routine be able to process the C output "as it comes in".
However, when you use a named pipe as output (or anything
that isn't an "interactive device" or emulation thereof), unix will
"fully buffer" the output, so unless the C program takes care to flush
the buffer, the matlab routine would only be able to read the data
in buffer-fulls rather than as it was generated. If the calculations
take a significant time between outputs, this can be a big problem.

In unix, when the output -is- to an "interactive device" (or
emulation thereof such as a pty), the C I/O library defaults to
"line buffered" output.

If the C program can be modified, then it is {probably} not difficult to
add a setvbuf() call to force line buffering.
--
  "The Romans believed that every man had his Genius, and every
   woman her Juno." -- Thomas Bulfinch

Subject: Read from a pipe (stdin)

From: Brian

Date: 26 Oct, 2009 00:35:02

Message: 4 of 4

"Ed Scheinerman" <ers@jhu.edw> wrote in message <g4ag9e$h2a$1@fred.mathworks.com>...
> The "data" I'd like to read into my matlab program is the
> output of a C program. I'd like to be able to pipe the
> output of the C program into matlab and have my .m file
> process that data as it comes in.
>
> I know I can write the output of the C program to disk and
> then read that file in. But the output is likely to be very
> large so I don't want to use that route.
>
> Is this possible? If so, how?
>
> Thanks.
>

Try: popen mex

http://www.mathworks.com/matlabcentral/fileexchange/13851

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us