function [result status] = python(varargin)
%PYTHON execute python command and return the result.
%   PYTHON(PYTHONFILE) calls pytohn script specified by the file PYTHONFILE
%   using appropriate pytohn executable.
%
%   PYTHON(PYTHONFILE,ARG1,ARG2,...) passes the arguments ARG1,ARG2,...
%   to the pytohn script file PYTHONFILE, and calls it by using appropriate
%   pytohn executable.
%
%   RESULT=PYTHON(...) outputs the result of attempted pytohn call.  If the
%   exit status of pytohn is not zero, an error will be returned.
%
%   [RESULT,STATUS] = PYTHON(...) outputs the result of the pytohn call, and
%   also saves its exit status into variable STATUS.
%
%   If the pytohn executable is not available, it can be downloaded from:
%     http://www.cpan.org
%
%   See also SYSTEM, JAVA, MEX.

%   Copyright 1990-2007 The MathWorks, Inc.
%   $Revision: 1.1.4.11 $

% Example: 
%
% # file:test.py
% import sys
% 
% def squared(x):
%     y = x * x
%     return y
% 
% if __name__ == '__main__':
%     x = float(sys.argv[1])
%     sys.stdout.write(str(squared(x)))
%
% file can be run as: python('test.py','10')

systemUnsetLD_Path = 'unset LD_LIBRARY_PATH; ';
cmdString = '';
bRunSeparateProcess = 0;

% Add input to arguments to operating system command to be executed.
% (If an argument refers to a file on the MATLAB path, use full file path.)
for i = 1:nargin
    thisArg = varargin{i};
    if ~ischar(thisArg)
        error(message('InputsMustBeStrings'));
    end
    if i==1
        if exist(thisArg, 'file')==2
            % This is a valid file on the MATLAB path
            if isempty(dir(thisArg))
                % Not complete file specification
                % - file is not in current directory
                % - OR filename specified without extension
                % ==> get full file path
                thisArg = which(thisArg);
            end
        else
            % First input argument is pytohnFile - it must be a valid file
            error(message('FileNotFound', thisArg));
        end
    end
    
    % Wrap thisArg in double quotes if it contains spaces
    if isempty(thisArg) || any(thisArg == ' ')
        thisArg = ['"', thisArg, '"']; %#ok<AGROW>
    end

    % Add argument to command string
    if strcmp(thisArg ,'&') % the character & means run separate process
        bRunSeparateProcess = 1;
    else
        cmdString = [cmdString, ' ',thisArg]; %#ok<AGROW>
    end
end

% Execute pytohn script
if isempty(cmdString)
    error(message('NoPythonCommand'));
elseif ispc
    % PC
    %pythonCmd = fullfile(matlabroot, 'sys\python\win32\bin\');
    pythonCmd = 'C:\Program Files\WinPython-64bit-2.7.5.3\python-2.7.5.amd64';
    cmdString = ['python' cmdString];
    pythonCmd = ['set PATH=',pythonCmd, ';%PATH%&' cmdString];
    [status, result] = dos(pythonCmd);
else
    % UNIX
    if bRunSeparateProcess
        cmdString = [cmdString, ' & '];
    end
        
    [status ignore] = unix('which python'); %#ok
    if (status == 0)
        cmdString = [systemUnsetLD_Path, 'python', cmdString];
        [status, result] = unix(cmdString);
    else
        error(message('MATLAB:python:NoExecutable'));
    end
end

% Check for errors in shell command
if nargout < 2 && status~=0
    error('Error to run python string. \n\n %s \n input: %s', result, cmdString);
end