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 );

更多回答(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