annotate core/tools/python.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
rev   line source
wolffd@0 1 function [result status] = python(varargin)
wolffd@0 2 %python Execute python command and return the result.
wolffd@0 3 % python(pythonFILE) calls python script specified by the file pythonFILE
wolffd@0 4 % using appropriate python executable.
wolffd@0 5 %
wolffd@0 6 % python(pythonFILE,ARG1,ARG2,...) passes the arguments ARG1,ARG2,...
wolffd@0 7 % to the python script file pythonFILE, and calls it by using appropriate
wolffd@0 8 % python executable.
wolffd@0 9 %
wolffd@0 10 % RESULT=python(...) outputs the result of attempted python call. If the
wolffd@0 11 % exit status of python is not zero, an error will be returned.
wolffd@0 12 %
wolffd@0 13 % [RESULT,STATUS] = python(...) outputs the result of the python call, and
wolffd@0 14 % also saves its exit status into variable STATUS.
wolffd@0 15 %
wolffd@0 16 % See also SYSTEM, JAVA, MEX.
wolffd@0 17
wolffd@0 18 % Copyright 1990-2007 The MathWorks, Inc.
wolffd@0 19 % $Revision: 1.1.4.8 $
wolffd@0 20
wolffd@0 21 cmdString = '';
wolffd@0 22
wolffd@0 23 % Add input to arguments to operating system command to be executed.
wolffd@0 24 % (If an argument refers to a file on the MATLAB path, use full file path.)
wolffd@0 25 for i = 1:nargin
wolffd@0 26 thisArg = varargin{i};
wolffd@0 27 if isempty(thisArg) || ~ischar(thisArg)
wolffd@0 28 error('MATLAB:perl:InputsMustBeStrings', 'All input arguments must be valid strings.');
wolffd@0 29 end
wolffd@0 30 if i==1
wolffd@0 31 if exist(thisArg, 'file')==2
wolffd@0 32 % This is a valid file on the MATLAB path
wolffd@0 33 if isempty(dir(thisArg))
wolffd@0 34 % Not complete file specification
wolffd@0 35 % - file is not in current directory
wolffd@0 36 % - OR filename specified without extension
wolffd@0 37 % ==> get full file path
wolffd@0 38 thisArg = which(thisArg);
wolffd@0 39 end
wolffd@0 40 else
wolffd@0 41 % First input argument is pythonFile - it must be a valid file
wolffd@0 42 error('MATLAB:perl:FileNotFound', 'Unable to find python file: %s', thisArg);
wolffd@0 43 end
wolffd@0 44 end
wolffd@0 45
wolffd@0 46 % Wrap thisArg in double quotes if it contains spaces
wolffd@0 47 if any(thisArg == ' ')
wolffd@0 48 thisArg = ['"', thisArg, '"'];
wolffd@0 49 end
wolffd@0 50
wolffd@0 51 % Add argument to command string
wolffd@0 52 cmdString = [cmdString, ' ', thisArg];
wolffd@0 53 end
wolffd@0 54
wolffd@0 55 % Execute python script
wolffd@0 56 errTxtNoPerl = 'Unable to find python executable.';
wolffd@0 57
wolffd@0 58 if isempty(cmdString)
wolffd@0 59 error('MATLAB:perl:NoPerlCommand', 'No python command specified');
wolffd@0 60 elseif ispc
wolffd@0 61 % PC
wolffd@0 62 perlCmd = []; % fullfile('C:\Program Files (x86)\Python3x');
wolffd@0 63 cmdString = ['python' cmdString];
wolffd@0 64 perlCmd = ['set PATH=',perlCmd, ';%PATH%&' cmdString];
wolffd@0 65 [status, result] = dos(perlCmd);
wolffd@0 66 else
wolffd@0 67 [status ignore] = unix('which python'); %#ok
wolffd@0 68 if (status == 0)
wolffd@0 69 cmdString = ['python', cmdString];
wolffd@0 70 [status, result] = unix(cmdString);
wolffd@0 71 else
wolffd@0 72 error('MATLAB:python:NoExecutable', errTxtNoPython);
wolffd@0 73 end
wolffd@0 74 end
wolffd@0 75
wolffd@0 76 % Check for errors in shell command
wolffd@0 77 if nargout < 2 && status~=0
wolffd@0 78 error('MATLAB:perl:ExecutionError', ...
wolffd@0 79 'System error: %sCommand executed: %s', result, cmdString);
wolffd@0 80 end
wolffd@0 81