0
votes

I have a python code where I'm using numpy to do some calculations. In my code I have some integer variable 'N' I use as an index.

I then use scipy.io.savemat to use this index in my Matlab (2017a) code, and I get this error when doing something like that:

% N is the int variable I have from my python code
>> N
N =
  int64
   1792
>> (1:2)/N
Error using  / 
Integers can only be combined with integers of the same class, or scalar doubles.

Apparently Matlab "native integers" are of class 'double'. By which I mean:

>> N=3
N =
     3
>> class(N)
ans =
    'double'

Had I assigned 'N' in my matlab code I wouldn't have problems doing the above code. But I assign my variables in python then convert them to matlab. Trying to use numpy.double for 'N' in my python code results in numpy warnings e.g.:

>>> N = numpy.double(100)
>>> numpy.zeros(N)
VisibleDeprecationWarning: using a non-integer number instead of an integer will result in an error in the future

To sum it up, I want to use 'N' as an integer in my python code, then as a double (or whatever works as a native matlab integer) in my matlab code. I thought savemat would do this conversion for me, but for probably other good reasons it doesn't.

The obvious solution would be to convert the integer to double before or after the serialization. But since I have a huge dictionary with many different variables, that would require tracking the types of each, which I'd really like to avoid. I'm looking for a simple more "native" solution.

How would you suggest solving this matter? thanks

1
np.double(100) produces a float. That's why np.zeros does not like it - it uses it but warns you. How about keeping N integer in Python, but saving it as float(N) to the file. I have Octave, not MATLAB, so may not be able to test this for you.hpaulj
Thanks @hpaulj, your solution would require tracking my types in my huge dictionary that I'm converting from python to matlab. You're that I wasn't clear about it in my question. I've updated edited it, please checknivniv

1 Answers

0
votes

As followup to my comment I tried saving some values in numpy, and loading them with octave:

In [552]: from scipy import io
In [553]: io.savemat('test.mat',dict(N=100, fN=float(100),x=np.arange(100)))

In octave

>> load test.mat
>> (1:2)/N
ans =
  0  0

>> (1:2)/fN
ans =
   0.010000   0.020000

>> (1:2)/100
ans =
   0.010000   0.020000
>> M=100
M =  100
>> whos
Variables in the current scope:

   Attr Name        Size                     Bytes  Class
   ==== ====        ====                     =====  =====
        M           1x1                          8  double
        N           1x1                          4  int32
        ans         1x2                         16  double
        fN          1x1                          8  double
        x           1x100                      400  int32

So yes, saving Python numbers (and arrays) as floats is the closest thing to writing those values in MATLAB/Octave.

>> (1:2)/double(N)
ans =
   0.010000   0.020000

also works to convert the imported values to float