How do I get the commands UNIX() and DOS() to work as POPEN does in Unix?

2 views (last 30 days)
How do I get the UNIX and DOS commands to work as POPEN does in Unix?
It seems the UNIX and DOS functions are missing one key feature. These functions capture the output of an executable, but there is no way to supply an input to the executable.
I would like to have the same ability as the 'popen' system call in Unix.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 27 Jun 2009
Programs executed using UNIX and DOS commands which use standard input expect stream input. MATLAB can currently stream to a file using commands like FWRITE and FPRINTF, but not to a pipe unless you are willing to write a MEX-file to do it. In this case, you have to combine the creation of the stream and the piping in the same MEX-file.
Suppose you have a two dimensional array of doubles called "data" that you want to stream as standard input to an application called "prog". Suppose also you want to stream one value per line, by columns, using the FPRINTF format.
Currently in MATLAB you can do the following:
file = tempname;
fid = fopen(file,'wt');
n = fprintf(fid,'%f\n',data);
fclose(fid);
if isunix
[status,results] = unix(['app < ' file]);
else
[status,results] = dos(['app < ' file]);
end
delete(file)
'Piping' was invented so that files would not have to be produced like this. This is replaced by communicating processes. The following is ideal:
1. Complete flexibility on the format of the stream.
2. Input and output from a single command
One possible idea is for MATLAB to support something like this:
s = ostream(func,arg,...);
[status,output] = pipe(s,cmd);
close(s);
Where "func" is a MATLAB function to create the stream. However, there would be a host of issues to solve and no changes are anticipated along these lines in the near future.
In summary:
UNIX and DOS commands use files and arguments to pass in data. The application drives how that data is to be presented to the program. If the data must come from standard input, use some form of MATLAB file I/O to write the data to a file and then issue a UNIX or DOS command to do the work referencing that file.

More Answers (0)

Categories

Find more on Data Import and Analysis in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!