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

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Reading a pipe or ouput of a C program into Matlab

Asked by Sampriti Bhattacharyya on 29 Nov 2012

I want to read out a pipe into matlab, but this isn't working. I have a windows computer

>>NET.addAssembly('System');
>> pipeStream = System.IO.Pipes.NamedPipeServerStream('testpipe', System.IO.Pipes.PipeDirection.Out)
Undefined variable "System" or function "System.IO.Pipes.PipeDirection.Out".

Then I tried popen

   popen('C:\Documents\ServerPipe\ServerPipe.exe')
  Undefined function 'popen' for input arguments of type 'char'.
>> popen(C:\Documents\ServerPipe\ServerPipe.exe)
          |
  Error: Unexpected MATLAB operator.

Infact poen doesnt exist in windows i think.

And yet, dateObj = System.DateTime.Now works fine! Help please! I need to get it working!

0 Comments

Sampriti Bhattacharyya

Products

No products are associated with this question.

2 Answers

Answer by Walter Roberson on 29 Nov 2012

The popen() system call does exist on MS Windows XP SP2 and later, but MATLAB does not offer an interface to that system call.

Unless you can find a way to use ActiveX, you would need to mex up a pipe interface.

Please note that MATLAB's built-in fwrite() and other I/O functions do not flush buffers, and that it is not certain that MATLAB's fseek() flushes buffers and there is no flush call in MATLAB. (These problems might possibly have changed in R2012b: work was done to allow diary() to flush more quickly, and that work might possibly imply something about flushing in general.)

4 Comments

Walter Roberson on 30 Nov 2012

Which compiler did you select with mex -setup ?

Sampriti Bhattacharyya on 4 Dec 2012

I use 2010 VC++ .

Was thinking perhaps to use memmapfile to access the data?

Walter Roberson on 4 Dec 2012

memmapfile() cannot be used to access pipes.

Walter Roberson
Answer by Sampriti Bhattacharyya on 5 Dec 2012
Edited by Walter Roberson on 5 Dec 2012

Walter, you are right. Could not figure out to use mex in this case, so tried out memmapfile. memmapfile can be used to access the output of a C code. I am reading output in real time, so I have a C program that reads it from the serial port and matlab which at the same time access the data which the C program is storing in a file. This might be useful for anyone, so here's the command

>>objname = memmapfile('Filename with filepath', ...
'Format', { 'uint64' [1] 'time'; 'double' [3] 'acc'; 'double' [3] 'ang'})
>>objname.Writable=true;

Thanks again!

0 Comments

Sampriti Bhattacharyya

Contact us