Daniel@0: function [result status] = python(varargin) Daniel@0: %python Execute python command and return the result. Daniel@0: % python(pythonFILE) calls python script specified by the file pythonFILE Daniel@0: % using appropriate python executable. Daniel@0: % Daniel@0: % python(pythonFILE,ARG1,ARG2,...) passes the arguments ARG1,ARG2,... Daniel@0: % to the python script file pythonFILE, and calls it by using appropriate Daniel@0: % python executable. Daniel@0: % Daniel@0: % RESULT=python(...) outputs the result of attempted python call. If the Daniel@0: % exit status of python is not zero, an error will be returned. Daniel@0: % Daniel@0: % [RESULT,STATUS] = python(...) outputs the result of the python call, and Daniel@0: % also saves its exit status into variable STATUS. Daniel@0: % Daniel@0: % See also SYSTEM, JAVA, MEX. Daniel@0: Daniel@0: % Copyright 1990-2007 The MathWorks, Inc. Daniel@0: % $Revision: 1.1.4.8 $ Daniel@0: Daniel@0: cmdString = ''; Daniel@0: Daniel@0: % Add input to arguments to operating system command to be executed. Daniel@0: % (If an argument refers to a file on the MATLAB path, use full file path.) Daniel@0: for i = 1:nargin Daniel@0: thisArg = varargin{i}; Daniel@0: if isempty(thisArg) || ~ischar(thisArg) Daniel@0: error('MATLAB:perl:InputsMustBeStrings', 'All input arguments must be valid strings.'); Daniel@0: end Daniel@0: if i==1 Daniel@0: if exist(thisArg, 'file')==2 Daniel@0: % This is a valid file on the MATLAB path Daniel@0: if isempty(dir(thisArg)) Daniel@0: % Not complete file specification Daniel@0: % - file is not in current directory Daniel@0: % - OR filename specified without extension Daniel@0: % ==> get full file path Daniel@0: thisArg = which(thisArg); Daniel@0: end Daniel@0: else Daniel@0: % First input argument is pythonFile - it must be a valid file Daniel@0: error('MATLAB:perl:FileNotFound', 'Unable to find python file: %s', thisArg); Daniel@0: end Daniel@0: end Daniel@0: Daniel@0: % Wrap thisArg in double quotes if it contains spaces Daniel@0: if any(thisArg == ' ') Daniel@0: thisArg = ['"', thisArg, '"']; Daniel@0: end Daniel@0: Daniel@0: % Add argument to command string Daniel@0: cmdString = [cmdString, ' ', thisArg]; Daniel@0: end Daniel@0: Daniel@0: % Execute python script Daniel@0: errTxtNoPerl = 'Unable to find python executable.'; Daniel@0: Daniel@0: if isempty(cmdString) Daniel@0: error('MATLAB:perl:NoPerlCommand', 'No python command specified'); Daniel@0: elseif ispc Daniel@0: % PC Daniel@0: perlCmd = []; % fullfile('C:\Program Files (x86)\Python3x'); Daniel@0: cmdString = ['python' cmdString]; Daniel@0: perlCmd = ['set PATH=',perlCmd, ';%PATH%&' cmdString]; Daniel@0: [status, result] = dos(perlCmd); Daniel@0: else Daniel@0: error('MATLAB:python:NoExecutable', errTxtNoPerl); Daniel@0: end Daniel@0: Daniel@0: % Check for errors in shell command Daniel@0: if nargout < 2 && status~=0 Daniel@0: error('MATLAB:perl:ExecutionError', ... Daniel@0: 'System error: %sCommand executed: %s', result, cmdString); Daniel@0: end Daniel@0: