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

7 visualizaciones (últimos 30 días)
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.

Respuesta aceptada

per isakson
per isakson el 1 de Feb. de 2013
Editada: per isakson el 1 de Feb. de 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 comentarios
K E
K E el 1 de Feb. de 2013
Editada: K E el 1 de Feb. de 2013
Thanks so much - that worked. Note the named pipe approach was faster (2s rather than 13s).
per isakson
per isakson el 3 de Feb. de 2013
Editada: per isakson el 3 de Feb. de 2013
I learned something!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre FPGA, ASIC, and SoC Development en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by