1

I am new to Python and I would like to pass my matlab variables to Python. The following code takes a python script and arguments. For example: python('square.py', '5'). Would it be possible to pass variables to Python through this matlab script? For example:

python('square.py', 'value')
function [result status] = python(varargin)

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 isempty(thisArg) || ~ischar(thisArg)
    error('MATLAB:python:InputsMustBeStrings', 'All input arguments must be valid strings.');
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('MATLAB:python:FileNotFound', 'Unable to find Python file: %s', thisArg);
    end
end

  % Wrap thisArg in double quotes if it contains spaces
  if any(thisArg == ' ')
    thisArg = ['"', thisArg, '"'];
  end

  % Add argument to command string
  cmdString = [cmdString, ' ', thisArg];
end

% Execute Python script
errTxtNoPython = 'Unable to find Python executable.';

if isempty(cmdString)
  error('MATLAB:python:NoPythonCommand', 'No python command specified');
   elseif ispc
  % PC
  pythonCmd = '/usr/bin/python';
  cmdString = ['python', cmdString];
  pythonCmd = ['set PATH=',pythonCmd, ';%PATH%&' cmdString];
  [status, result] = dos(pythonCmd);
else
  % UNIX
  [status ignore] = unix('which python'); %#ok
  if (status == 0)
    cmdString = ['python', cmdString];
    [status, result] = unix(cmdString);
  else
    error('MATLAB:python:NoExecutable', errTxtNoPython);
  end
end

% Check for errors in shell command
if nargout < 2 && status~=0
  error('MATLAB:python:ExecutionError', ...
    'System error: %sCommand executed: %s', result, cmdString);
end
3
  • Is there a specific question you have? Commented Dec 4, 2012 at 4:27
  • yes, how do I convert the above code, to one which accepts variables? The following code accepts string values and not string variables. Commented Dec 4, 2012 at 12:48
  • python('sqd.py','A') ??? Error using ==> python at 83 System error: Traceback (most recent call last): File "sqd.py", line 9, in <module> x = eval(sys.argv[1]) File "<string>", line 1, in <module> NameError: name 'A' is not defined Command executed: python sqd.py A Commented Dec 4, 2012 at 14:05

1 Answer 1

2

For a small amount of numbers, and based on your code, you can write something based on printf and basically pass through a formatted string of numbers to the python routine. The python routine needs to parse it back into numbers.

For larger junks of data, you may write it into temporary files (either as csv or maybe even as binaries with fwrite) and pass through the file name. If you're running Linux and having e.g. /tmp mounted as an tmpfs (like a ram-disk), using it as temporary storage speeds up the process.

There are also people, who use UNIX-pipes between Python and Matlab. As an example http://danapeerlab.googlecode.com/svn/trunk/freecell/depends/common/python/matlabpipe.py

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.