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

4 visualizzazioni (ultimi 30 giorni)
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.

Risposta accettata

per isakson
per isakson il 1 Feb 2013
Modificato: per isakson il 1 Feb 2013
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 Commenti
K E
K E il 1 Feb 2013
Modificato: K E il 1 Feb 2013
Thanks so much - that worked. Note the named pipe approach was faster (2s rather than 13s).

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su FPGA, ASIC, and SoC Development in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by