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