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
np.double(100)
produces a float. That's whynp.zeros
does not like it - it uses it but warns you. How about keepingN
integer in Python, but saving it asfloat(N)
to the file. I have Octave, not MATLAB, so may not be able to test this for you. – hpaulj