Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sprintf MATLAB and Python

Tags:

python

matlab

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) 
like image 667
Carnez Davis Avatar asked Dec 06 '25 06:12

Carnez Davis


1 Answers

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);
like image 77
Joseph Stine Avatar answered Dec 07 '25 20:12

Joseph Stine



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!