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