Redirect output from C to Matlab

1 view (last 30 days)
Nikolay Konstantinov
Nikolay Konstantinov on 15 Jan 2018
Commented: Walter Roberson on 15 Jan 2018
I'm using Linux & Matlab R2017.
I have a C program, which have an infinite output via
printf()
which I want to direct to matlab.
I'm able to run this program like this
unix('./prog')
after that I have output, but I dont know how to save it to matlab array.
Actually, I want to make it like FIFO, but no idea how.. As I know in matlab no pipes like in bash.

Answers (1)

Walter Roberson
Walter Roberson on 15 Jan 2018
[status, message] = system('./prog 2>&1');
Now the output will be in message
There is a popen() contribution in the File Exchange, but it is only single direction.
  2 Comments
Nikolay Konstantinov
Nikolay Konstantinov on 15 Jan 2018
Edited: Nikolay Konstantinov on 15 Jan 2018
Thank you for answer! Yes, it's true. But I want processing it real-time, but I got message array only after interrupting.
There is another approach, I've made a pipe. and run C program at terminal
./prog > PIPE;
in C I have
fd = open(portname, O_RDWR | O_NOCTTY | O_SYNC);
for (p = buf; rdlen-- > 0; p++)
printf("%hu ", *p);
Then I'm able to read it in matlab, just like a file. But how can I flush it?
Walter Roberson
Walter Roberson on 15 Jan 2018
MATLAB is weak on flushing. It exists for the Instrument Control Toolbox, which you are not using. Otherwise you could experiment with using ftell() to determine the current size and then fseek() to that size, but I am not confident that would work with a pipe.

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!