Calling Matlab function from Python: "Initializer must is a rectangular nested sequence"

Source: Internet
Author: User
Tags python script



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 filetest.txt.



This file is imported as arraydata. In MATLAB, the Anovan function would is called as follows:


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


In Python, I has 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 was some issue in passing the last both array columns to the function, but am unsure of the solution. Any suggestions would is appreciated.






I have now worked this out. The issue is 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)])


Calling Matlab function from Python: ' initializer must be a rectangular nested sequence '


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.