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

2 vues (au cours des 30 derniers jours)
K E
K E le 31 Jan 2013
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.

Réponse acceptée

per isakson
per isakson le 1 Fév 2013
Modifié(e) : per isakson le 1 Fév 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 commentaires
K E
K E le 1 Fév 2013
Modifié(e) : K E le 1 Fév 2013
Thanks so much - that worked. Note the named pipe approach was faster (2s rather than 13s).
per isakson
per isakson le 3 Fév 2013
Modifié(e) : per isakson le 3 Fév 2013
I learned something!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Simulink Functions dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by