-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython.m
More file actions
82 lines (75 loc) · 2.75 KB
/
Copy pathpython.m
File metadata and controls
82 lines (75 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
function [result, status] = python(varargin)
%PYTHON Execute python command and return the result.
% python(pythonFILE) calls python script specified by the file pythonFILE
% using appropriate python executable.
%
% python(pythonFILE,ARG1,ARG2,...) passes the arguments ARG1,ARG2,...
% to the python script file pythonFILE, and calls it by using appropriate
% python executable.
%
% RESULT=python(...) outputs the result of attempted python call. If the
% exit status of python is not zero, an error will be returned.
%
% [RESULT,STATUS] = python(...) outputs the result of the python call, and
% also saves its exit status into variable STATUS.
%
% If the python executable is not available, it can be downloaded from:
% http://www.cpan.org
%
% See also SYSTEM, JAVA, MEX.
% Copyright 1990-2012 The MathWorks, Inc.
% $Revision: 1.1.4.12 $
cmdString = '';
% 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('MATLAB:python: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 pythonFile - it must be a valid file
error(message('MATLAB:python: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
cmdString = [cmdString, ' ', thisArg]; %#ok<AGROW>
end
% Execute python script
if isempty(cmdString)
error(message('MATLAB:python:NopythonCommand'));
elseif ispc
% PC
%pythonCmd = fullfile(matlabroot, 'C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 2.7');
pythonCmd = fullfile(matlabroot, 'sys\python\win32\bin\');
cmdString = ['python' cmdString];
pythonCmd = ['set PATH=',pythonCmd, ';%PATH%&' cmdString];
[status, result] = dos(pythonCmd);
else
% UNIX
[status, ~] = unix('which python');
if (status == 0)
cmdString = ['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(message('MATLAB:python:ExecutionError', result, cmdString));
end