Piping gdalinfo.exe to matlab/octave with system() does not return any output - matlab

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?

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.

This works on my Windows machine if I am in the Octave directory:
[status output] = system('ls bin')

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.

Related

While running Compiled MatLab from console window, prompt returns immediately while app is still running

I have compiled my simple MatLab function so I can run it from a Windows console as a command line app. After I enter the command and its single argument, the prompt returns immediately even though the command is still running, as if running in the background.
Can someone tell me why this happens and how do I stop it? I'd like it to behave like a normal application and not return to the prompt until complete.
EDIT: Sorry I didn't post the code previously, I figured I was making a common mistake and would get a quick answer. Anyway, here it is.
It translates a .mat file to a .csv file and then runs another console command via "system" to perform a quick edit on the resulting .csv file. Several minutes of IO but not much else.
I've never compiled MatLab before so probably something simple, I hope.
function WriteMatToCSV( inFile, overWrite )
if ~exist(inFile, 'file')
fprintf('Error File not found: %s\n',inFile);
return;
end
overWriteExisting = 1;
if exist('overWrite','var')
overWriteExisting = overWrite;
end
% Change file extension
outFile = strrep( inFile, '.mat','.csv');
fprintf('In : %s\n',inFile);
fprintf('Out: %s\n',outFile);
% Overwrite if exists?
if exist(outFile, 'file')
if ~overWriteExisting
fprintf('Error File exists: %s\n',outFile);
return;
end
end
% Get variable name in the file
matObj = matfile(inFile);
info = whos(matObj);
name = info.name;
fprintf('Found variable: %s\n',name);
% Load the variable from the file
fprintf('Loading mat...\n');
load(inFile);
% Write the variable to .csv file
fprintf('Writing csv...\n');
export(eval(name),'File',outFile','Delimiter',',');
fprintf('Rewriting to remove MatLab bits...\n');
command = 'RewriteMatLabCSV';
command = [command ' ' outFile];
[status,cmdout] = system(command,'-echo');
fprintf(cmdout);
fprintf('Done\n');
end

calling an external program in matlab in a loop

I have installed Matlab 2014 in Ubuntu. My problem:
I build several input files for another program, Quantum Espresso, in Matlab. Now I should pass these files to Quantum Espresso using matlab command line. Now I know I can do this using Linux Terminal, but my way of solving my problem has reached the point that my only option is 'calling Quantum Espresso from matlab'. One single call is actually easy:
! installation/folder/espresso-5.3.0/bin/pw.x < inputfile > outputfile
The problem is I have several input files named like 1name.in 1name.in ... . So this repeated calls should be done in a loop. But how?
I have tried:
the shell script for looping though the files. I added that extra '!' to each line of the script but it doesn't work.
I also tried to write a loop like this :
for i = 1:N
prefix = int2str(i);
fuloutname = [prefix 'name' '.' 'out'];
fulinname = [prefix 'name' '.' 'in'];
! adress/espresso-5.3.0/bin/pw.x < fulinname > fuloutname ;
end
In which 'N' in number of my input files. Clearly running this means you are passing a file nemaed 'fulinname' not 1name.in and will result in an output file named 'fuloutname'
I also tried to do it as you normally load various files in a loop but it also did not work
Please help me.
You should use the unix function:
for i = 1:N
prefix = int2str(i);
fuloutname = [prefix 'name' '.' 'out'];
fulinname = [prefix 'name' '.' 'in'];
mycommand = ['adress/espresso-5.3.0/bin/pw.x < ',fulinname,' > ',fuloutname];
unix(mycommand);
%system(mycommand); %will give you the same, result and this function is cross-platform
end

Get command line arguments in matlab

This is probably too easy, but I cannot google the answer for this: how can I get command line arguments in matlab script.
I run matlab as matlab -nodisplay -r "run('script.m')" and I want to return all arguments as a list. Something similar to python sys.argv. How can I do this?
I'm using Linux Mint and MATLAB 2015a.
I came up with a simple function that works on both Windows and Linux (Ubuntu):
function args = GetCommandLineArgs()
if isunix
fid = fopen(['/proc/' num2str(feature('getpid')) '/cmdline'], 'r');
args = textscan(fid, '%s', 'Delimiter', char(0));
fclose(fid);
else
kernel32WasAlreadyLoaded = libisloaded('kernel32');
if ~kernel32WasAlreadyLoaded
temporaryHeaderName = [gettempfolder '\GetCommandLineA.h'];
dlmwrite(temporaryHeaderName, 'char* __stdcall GetCommandLineA(void);', '');
loadlibrary('kernel32', temporaryHeaderName);
delete(temporaryHeaderName);
end
args = textscan(calllib('kernel32', 'GetCommandLineA'), '%q');
if ~kernel32WasAlreadyLoaded
unloadlibrary kernel32;
end
end
args = args{1};
On your sample call, it would return this:
>> GetCommandLineArgs
args =
'/[path-to-matlab-home-folder]/'
'-nodisplay'
'-r'
'run('script.m')'
It returns a cell array of strings, where the first string is the path to MATLAB home folder (on Linux) or the full path to MATLAB executable (on Windows) and the others are the program arguments (if any).
How it works:
On Linux: the function gets the current Matlab process ID using the feature function (be aware it's an undocumented feature). And reads the /proc/[PID]/cmdline file, which on Linux gives the command line arguments of any process. The values are separated by the null character \0, hence the textscan with delimiter = char(0).
On Windows: the function calls GetCommandLineA, which returns the command line arguments on a string. Then it uses textscan to split the arguments on individual strings. The GetCommandLineA function is called using MATLAB's calllib. It requires a header file. Since we only want to use one function, it creates the header file on the fly on the temporary folder and deletes it after it's no longer needed. Also the function takes care not to unload the library in case it was already loaded (for example, if the calling script already loads it for some other purpose).
I am not aware of a direction solution (like an inbuilt function).
However, you can use one of the following workarounds:
1. method
This only works in Linux:
Create a file pid_wrapper.m with the following contents:
function [] = pid_wrapper( parent_pid )
[~, matlab_pid] = system(['pgrep -P' num2str(parent_pid)]);
matlab_pid = strtrim(matlab_pid);
[~, matlab_args] = system(['ps -h -ocommand ' num2str(matlab_pid)]);
matlab_args = strsplit(strtrim(matlab_args));
disp(matlab_args);
% call your script with the extracted arguments in matlab_args
% ...
end
Invoke MATLAB like this:
matlab -nodisplay -r "pid_wrapper($$)"
This will pass the process id of MATLAB's parent process (i.e. the shell which launches MATLAB) to wrapper. This can then be used to find out the child MATLAB process and its command line arguments which you then can access in matlab_args.
2. method
This method is OS independent and does not really find out the command line arguments, but since your goal is to pass additional parameters to a script, it might work for you.
Create a file vararg_wrapper.m with the following contents:
function [] = wrapper( varargin )
% all parameters can be accessed in varargin
for i=1:nargin
disp(varargin{i});
end
% call your script with the supplied parameters
% ...
end
Invoke MATLAB like this:
matlab -nodisplay -r "vararg_wrapper('first_param', 'second_param')"
This will pass {'first_param', 'second_param'} to vararg_wrapper which you can then forward to your script.

reading in all text files from specific directory

I started learning matlab very recently and am stuck on this issue.
My code looks like this:
fileloc = '/no_backup/GroupData/ESRL/Download_18Mar2014/surface/'
list = dir(fileloc, '*.txt')
numTextFiles = length(list)
disp(numTextFiles)
So far I am just checking to see if it has read in the correct amount of text files, but it gives me the error:
"Error using dir
Too many input arguments."
I tried replacing
list = dir(fileloc, '*.txt')
to
list = dir(strcat(fileloc, '*.txt'))
(just in case my syntax was completely wrong since I have no idea)
but then it prints
numTextFiles =
0
0
I am just wondering if any of the functions I'm using are inappropriate and if so, which ones would I use/how would I use those and how would I just display the number of text files I have in that folder?
the argument to dir should be directoryname/*.txt or directoryname\*.txt if you're on windows. you can achieve this with
list = dir([ fileloc filesep '*.txt' ]);
[] concatenates strings. filesep is a builtin that evaluates to a string of the separator for your OS.

opening a batch file that opens a text file in python

I am writing a script that can execute a batch file, which needs to open a file in the same folder first. My current code is:
from subprocess import Popen
p = Popen("Mad8dl.bat <RUNTHISTO.txt>", cwd=r"C:\...\test")
stdout, stderr = p.communicate()
where the ... is just the path to the folder. However, everytime I run it I get the syntax error:
The syntax of the command is incorrect
Any help regarding the syntax would be greatly appreciated.
First, you should probably remove the < and > angle brackets from your code; just pass the filename, without any brackets, to your batch file. (Unless your filename really does contain < and > characters, in which case I really want to know how you managed it since those characters are forbidden in filenames in Windows).
Second, your code should look like:
from subprocess import Popen, PIPE
p = Popen(["Mad8dl.bat", "RUNTHISTOO.txt"], cwd=r"C:\...\test", stdout=PIPE, stderr=PIPE)
stdout, stderr = p.communicate()
Note the list containing the components of the call, rather than a single string. Also note that you need to specify stdout=PIPE and stderr=PIPE in your Popen() call if you want to use communicate() later on.