0

I'm calling a matlab function in python through matlab engine, and I'm having problems to pass the variables. I have figured out how to pass some, but for this on I'm getting an error. should be a scalar int. but when I pass it I got the error:

File "C:\ProgramData\Anaconda3\lib\site-packages\matlab_internal\mlarray_utils.py", line 90, in _normalize_size if init_dims[0] == 0:

IndexError: tuple index out of range

The code works fine If I do not pass the modn variable, so I know that my problem is the conversion type to matlab of this variable.

this is the python code:

import numpy  as np
import matlab
import matlab.engine
eng =  matlab.engine.start_matlab()

eng.cd()

Nn = 30
x= 250*np.ones((1,Nn)) 
y= 100*np.ones((1,Nn)) 
z = 32.0
xx = matlab.double(x.tolist())
yy = matlab.double(y.tolist())

f=np.arange(start=0.1,stop=0.66,step=0.1)
modnv=np.concatenate((np.ones((Nn)),2*np.ones((Nn))))

count = 0
for fks in f:
    fks=np.float(0)
    modn = modnv[count]
    modn = modn.astype(int)
    modn = matlab.int8(modn)
    Output = eng.simple_test(xx,yy,z,fks,modn,nargout=4)
    A = np.array(Output[0]).astype(float)
    B = np.array(Output[1]).astype(float)
    C = np.array(Output[2]).astype(float)
    D = np.array(Output[3]).astype(float)
    count = count + 1

and this is the matlab function simple_test:

function [A,B,C,D] = simple_test(x,y,z,fks,modn)

if modn == 1
    A = 3*x+2*y;
    B = x*ones(length(x),length(x));
    C = ones(z);
    D = x*y';
else
    A = 3*fks;
    B = 3*x+2*y;
    C = A+B;
    D = x*y'
end

Does someone know how to overcome that?

JCV
  • 447
  • 1
  • 5
  • 15

1 Answers1

1

Whenever you get IndexError: tuple index out of range error its mostly:

  1. Probably one of the indexes is wrong.I suspect you mean to say [0] where you say [1] and [1] where you say [2]. Indexes are 0-based in Python.
  2. you are passing an array to a function that was expecting a variadic sequence of arguments (eg '{}{}'.format([1,2]) vs '{}{}'.format(*[1,2])
rmb
  • 553
  • 5
  • 15
  • the error is in a python build-in function, the mlarray_utils.py and is because of the type of input, if was an array with dimension 2 would be ok... File "C:\ProgramData\Anaconda3\lib\site-packages\matlab\_internal\mlarray_utils.py", line 90, in _normalize_size if init_dims[0] == 0: IndexError: tuple index out of range – JCV Jun 22 '20 at 11:34