4

I am writing a python script from which I hope to call the Matlab anovan function. I have attempted a basic test of this feature on file test.txt, which contains 3 columns:

1.472008228227131532e+01 1.000000000000000000e+00 1.000000000000000000e+00
1.423892587976163249e+01 2.000000000000000000e+00 1.000000000000000000e+00
1.434051782779769013e+01 3.000000000000000000e+00 1.000000000000000000e+00
1.478373164841650045e+01 4.000000000000000000e+00 1.000000000000000000e+00
1.463143763527341612e+01 5.000000000000000000e+00 1.000000000000000000e+00
1.580723346731706513e+01 6.000000000000000000e+00 1.000000000000000000e+00
1.964295632547030124e+01 1.000000000000000000e+00 2.000000000000000000e+00
1.922724696768608510e+01 2.000000000000000000e+00 2.000000000000000000e+00
1.886686329445662480e+01 3.000000000000000000e+00 2.000000000000000000e+00
1.960702674675711066e+01 4.000000000000000000e+00 2.000000000000000000e+00
1.983434584140020007e+01 5.000000000000000000e+00 2.000000000000000000e+00
2.050709640103466569e+01 6.000000000000000000e+00 2.000000000000000000e+00

This file is imported as array data. In matlab, the anovan function would be called as follows:

anovan(data(:,1),{data(:,2),data(:,3)})

In python, I have attempted to call the function like this:

import matlab.engine
eng = matlab.engine.start_matlab() 
data = np.genfromtxt('test.txt')
eng.anovan(matlab.double(data[:,0]),matlab.int8(data[:,1:2]))

Unfortunately, this gives the following error:

ValueError: initializer must be a rectangular nested sequence

I can see that there is some issue in passing the last two array columns to the function, but am unsure of the solution. Any suggestions would be appreciated.

crevell
  • 449
  • 6
  • 19
  • The slicing argument `1:2` only takes one column, namely `1`. You may try `1:3` or better just `1:` (everything from `1` to the end) instead. Side note: please provide the full traceback so everyone can be certain about where the error exactly comes from. – a_guest Jan 10 '17 at 13:28

2 Answers2

3

I have now worked this out. The issue was in the format of the data passed to the function.

#convert grouping columns to integer lists
list1 = data[:,1].tolist()
for i in range(0,len(list1)):                                                                                  
    list1[i] = int(list1[i])
list2 = data[:,2].tolist()
for i in range(0,len(list2)):                                                                                  
    list2[i] = int(list2[i])
#The following command now works
eng.anovan(matlab.double(data[:,0].tolist()),[matlab.int8(list1),matlab.int8(list2)])
crevell
  • 449
  • 6
  • 19
0

yes,it is,the parameter conveyed to the function in MATLAB should be in this type: matlab.double([parameter]), there needs a [] no matter what kind of type the parameter originally be