Is there a better way to capture the output of an external program?

조회 수: 2 (최근 30일)
In a Matlab script, I use a command-line program called wgrib2 to extract information from a compressed jpeg2000 file. Presently the information is extracted to a multi-line text file which I later read into Matlab. I save/read the file many times and I suspect that's why my script runs slowly. To avoid disk i/o, I'd like to capture the wgrib2 output to a Matlab variable; apparently this is not directly possible, but with mkfifo it might be possible. Here is how I am doing it, but if there is a better way, please let me know.
!mkfifo myfifo % Make a named pipe
% Call the external program and dump its output to the named pipe
system('wgrib2.exe multi_1.glo_30m.hs.201212.grb2 -ij 1 165 -ij 1 166 -ij 1 254 -ij 1 255 > myfifo &');
fid = fopen('myfifo','r'); % Send output to the named pipe as if it were a file
a = fscanf(fid, '%c'); % Read the output as character
fclose(fid); % Close the fake file
system('rm myfifo'); % Close the named pipe
strToSplitOn = ':val='; % Output values are separated by this sequence
% Split the output and convert to a number
variableWithFileOutput = str2double(regexp(a, strToSplitOn, 'split'));
FYI it is possible to "reuse" the named pipe so in my program where I call wgrib2 thousands of times, I put system('rm myfifo') at the end.

채택된 답변

per isakson
per isakson 2013년 2월 1일
편집: per isakson 2013년 2월 1일
These two lines are from my code. However, I don't know if it's better or even possible in your case
mlb_cmd = sprintf( 'dos( ''%s'' );', dos_cmd );
....
dos_buf = evalc( mlb_cmd );
  댓글 수: 2
K E
K E 2013년 2월 1일
편집: K E 2013년 2월 1일
Thanks so much - that worked. Note the named pipe approach was faster (2s rather than 13s).
per isakson
per isakson 2013년 2월 3일
편집: per isakson 2013년 2월 3일
I learned something!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Simulink Functions에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by