3

I am trying to pass a matrix variable from MATLAB to python, but I am only acquiring the first element of that matrix in python. Does anyone know can get the full matrix?

Python

import sys


if __name__ == '__main__':
    x = sys.argv[1]
    print x

MATLAB

A = magic(5);
[str,err] = sprintf('/usr/local/python name_of_program.py %d ', A);
unix(str) 
2
  • if you change the line x = sys.argv[1] to sys.argv, what is the output? Commented Dec 4, 2012 at 19:11
  • I get the following: ['sqd.py', '16', '/usr/bin/python2.6', 'sqd.py', '5', '/usr/bin/python2.6', 'sqd.py', '9', '/usr/bin/python2.6', 'sqd.py', '4', '/usr/bin/python2.6', 'sqd.py', '2', '/usr/bin/python2.6', 'sqd.py', '11', '/usr/bin/python2.6', 'sqd.py', '7', '/usr/bin/python2.6', 'sqd.py', '14', '/usr/bin/python2.6', 'sqd.py', '3', '/usr/bin/python2.6', 'sqd.py', '10', '/usr/bin/python2.6', 'sqd.py', '6', '/usr/bin/python2.6', 'sqd.py', '15', '/usr/bin/python2.6', 'sqd.py', '13', '/usr/bin/python2.6', 'sqd.py', '8', '/usr/bin/python2.6', 'sqd.py', '12', '/usr/bin/python2.6', 'sqd.py', '1'] Commented Dec 4, 2012 at 19:46

2 Answers 2

1

Look at the contents of str: /usr/local/python name_of_program.py 17 /usr/local/python name_of_program.py 23 /usr/local/python name_of_program.py 4 ...

When you pass a 5x5 matrix to sprintf, it reproduce the formatting string 25 times, with one of the elements substituted for the %d, in order (column by column).

I suggest another way to transfer data between the programs, such as writing it to a file. If you really want to pass everything on the command line, try the following:

A_str = sprintf(' %d',A);
str = strcat('/usr/local/python name_of_program.py ',A_str);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but with it, it seems to concatenating the name of file and the first element even with a space. I got the following ??? Error using ==> python at 69 System error: /usr/bin/python: can't open file 'sqd.py16': [Errno 2] No such file or directory Command executed:
0

You might want to use numpy to reshape the array, too. Or if it's too much to import, you could make your own reshape function Try this:

import sys, numpy
if __name__ == '__main__':
    x = sys.argv[1::3]
    y = numpy.reshape(x, (5,5))
    print y

I also notice that the call is adding the string once for each element of A, so you might consider using mat2str(A), like:

[str, err] = sprintf('/usr/local/python program.py "%s" ', mat2str(A));

I've also changed the %d to %s, because you're passing it as a string on the python anyway. The output of this is:

python program.py "[17 24 1 8 15;23 5 7 14 16;4 6 13 20 22;10 12 19 21 3;11 18 25 2 9]"

You can make a matrix from that string with numpy as well

>>> import sys
>>> import numpy

>>> print(numpy.matrix(sys.argv[1]))

matrix([[17, 24,  1,  8, 15],
    [23,  5,  7, 14, 16],
    [ 4,  6, 13, 20, 22],
    [10, 12, 19, 21,  3],
    [11, 18, 25,  2,  9]])

7 Comments

Thanks, but, how could it be adopted for uneven dimensions? (4x6)
The reshape function could be called like y = numpy.reshape(x, (4,4)). The data is passed to argv without any shape information, so you either need to know what shape you're taking in, or pass that along as an argument as well.
Thank you, but if I had an uneven dimensions, how would that effect the sys.arvg[1::3]? I understand the reshape statement.
Uneven dimensions wouldn't affect that line. The 1 is the position of the first matrix element and the 3 says to use every 3rd element. By your output, it looks like that will cover all cases, because the matrix elements are every 3rd element of argv. The double colon means to keep selecting elements until the end.
It seems that for uneven dimensions, there are extra elements in the set. MATLAB 0.1564 0.8555 0.6448 0.3763 python [['1.564050e-01' '8.555228e-01' '6.447645e-01' '3.762722e-01' '1.909237e-01' '4.282530e-01'] The '1.909237e-01' '4.282530e-01' are added?
|

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.