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?

5
  • Try removing nargout argument from eng.simple_test() Commented Jun 20, 2020 at 14:03
  • Did you try modn = matlab.int8([modnv[count]])? Commented Jun 20, 2020 at 16:54
  • @CrisLuengo I try what you said and I got the error: TypeError: integer argument expected, got float Commented Jun 22, 2020 at 11:29
  • @Anwarvic I tried, didn't change the error. :( Commented Jun 22, 2020 at 11:35
  • Ok, then cast to integer. The key in my comment was putting the value in a list. MATLAB doesn’t know integers, it only knows arrays. Try modn = matlab.int8([modnv[count].astype(int)]). Commented Jun 22, 2020 at 13:38

1 Answer 1

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])
Sign up to request clarification or add additional context in comments.

1 Comment

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

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.