0

I am trying to use the system() function to pipe the output of the gdalinfo (version 1.10 x64) utility directly to Matlab/Octave. The function consistently returns status=0, but does not return any output. For example:

[status output] = system('"C:\Program Files\GDAL\gdalinfo.exe" "E:\DATA\image.tif"')

will only return:

status =
    0
output =
    ''

Any idea why no output is returned?

jatobat
  • 749
  • 3
  • 8
  • 21
  • The output is what the command prints out to the [standard output](http://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29). `cd ..` doesn't print anything... what do you expect `output` to be? – Eitan T Jul 08 '13 at 14:27
  • Try `[status output] = system('dir')` – Hugh Nolan Jul 08 '13 at 14:36
  • Status = 0 indicates no error occurred. – Hugh Nolan Jul 08 '13 at 14:37
  • Well, this was maybe not a good example. I am using `system()` to execute the `gdalinfo.exe` utility which provides information about geographic raster files. This is the command I want to execute `"C:\Program Files\GDAL\gdalinfo.exe" "E:\DATA\image.tif"`. It works fine in the command line. But for some reason, the same command sent through the `system()` function indicates status=0 but returns no output. – jatobat Jul 08 '13 at 14:45

3 Answers3

1

It appears there is something strange about `gdalinfo.exe'. Several people have reported difficulty piping the output of the program to a textfile - see for example http://osgeo-org.1560.x6.nabble.com/GDALINFO-cannot-pipe-to-text-file-td3747928.html

So the first test would be - can you do something like this:

"C:\Program Files\GDAL\gdalinfo.exe" "E:\DATA\image.tif" > myFile.txt

and see whether the file is created and has any content? If it doesn't, it may be that the program is using a different way to produce output (for example, using stderr instead of stdout). If it is possible to get data into a text file but not directly to matlab, I suppose a workaround would be to write to file, then read that file in separately:

tempFile = tempname; % handy built in function to create temporary file name
execCmd = '"C:\Program Files\GDAL\gdalinfo.exe ';
targetFile = '"E:\DATA\image.tif"';
status = system([execCmd targetFile ' > ' tempFile]);
output = textread( tempFile, '%s' );
system(['del ' tempFile);

Now the output variable will be a cell array with one cell per line in the input file.

Floris
  • 45,857
  • 6
  • 70
  • 122
  • Indeed, it seems to be impossible to pipe the output of `gdalinfo` (GDAL-1.10.0.win-amd64-py2.7) to a textfile. Strangely enough, I've installed an older release (GDAL-1.9.2.win-amd64-py2.7) and piping works fine (including with the `system()` function). I guess this means a ticket for the current release. – jatobat Jul 08 '13 at 20:38
  • Thanks for the insight into the `gdal` issue. I've modified my initial question to make it more specific to `gdalinfo`. – jatobat Jul 08 '13 at 20:54
0

This works on my Windows machine if I am in the Octave directory:

[status output] = system('ls bin')
Doctor Dan
  • 771
  • 4
  • 11
  • @jatobat: `system()` is only called with one input argument here! The string `'ls bin'` is one argument, not two. – Floris Jul 08 '13 at 15:55
  • 1
    I edited my original response. As an aside, if you wanted to concatenate two commands in `system`, you could use `&` as in `[output text] = system('cd bin & pwd')`. – Doctor Dan Jul 08 '13 at 16:02
  • @Floris Yes right now it is one argument because @Doctor Dan edited his previous answer which had two function arguments, after my comment. By the way, I do get an output for `[status output] = system('dir C:\')`. Maybe I should reformulate my question and make it specific to the `gdal` utilities. – jatobat Jul 08 '13 at 16:04
  • 1
    This sounds like it specific to the utility you are using, rather than the Matlab command. Sounds like maybe the output is not using stdout or stderr. if so, you could try using something like this: `system('"C:\Program Files\GDAL\gdalinfo.exe" "E:\DATA\image.tif" 3>&1');` and try iterating through 3 to 9, which are available to applications to define their own outputs (from: http://stackoverflow.com/questions/1420965/redirect-stdout-and-stderr-to-a-single-file-in-dos and http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/redirection.mspx?mfr=true) – Hugh Nolan Jul 08 '13 at 16:48
  • @Hugh Nolan Thanks for the comment. Unfortunately, this doesn't seem to work. – jatobat Jul 08 '13 at 20:32
0

I was having the same issue trying to pipe the output from within C#. It turns out that the ECW plugin breaks the capability (I don't know how). If this plugin is not crucial to you, go into the plugins directory and delete gdal_ECWJP2ECW.dll. You should be able to use '>' and other stuff to dump your output to a file.

Avezou
  • 91
  • 1
  • 6