Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
From Matlab to Python
#1
Hi all!

I am working on a sound classification project. I have written codes in Matlab, but I need to convert it into Python to use with Raspberry Pi. However, I am not very experienced in Python, so I could not convert some parts of the code.

Here is the Matlab version of the code;

"
clc
close all

load output.mat
d=2*44100;
m=1;
for i=2:41
C1=output{i,1};
r= floor(size(C1,1)/d);
for j=1:r
C1_Data(m,:)=C1(1+((j-1)*d):(d+((j-1)*d)));
m=m+1;
end
"

Here is the Python version which is created by me. However, I could not avoid errors.

"

output = np.concatenate((a1,a2,a3,a4,a5,a6,a7,a8,a9))

d=244100
m=1

C1_Data = pd.DataFrame(reweightTarget, columns = ('Sound Class'))

for i in range(0,43):
C1 = output[i][0]
r = math.floor(C1.size/d)
for j in range (1,r):
s=C1[1+((j-1)d):(d+((j-1)*d))]
C1_Data.loc[m]=[s]
m +=1 "
The error

"index(...) must be called with a collection of some kind 'Sound Class' was passed"


If you can help me solve the problem, I will be grateful.
Reply
#2
First, please post code using the Python code marker on the editor toolbar. Blue and yellow python symbol. Otherwise indentation is lost and there may be indentation problems.

The error you get is that the columns = part of your dataframe definition has a single item, where a collection (list, for example) is necessary. If you put brackets [] around your 'Sound Class' that error will go away.

Other issues -
Minor point - range(0,43) the 0 is not needed
Single letter variable names make the code less readable and maintainable. Python style suggests variable names that describe the variable.
erbab likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Media Pipe Python Interfacing with MATLAB cmcreecc 1 975 May-30-2024, 07:23 AM
Last Post: TrentErnser
Photo Matlab to Python Sateesh 0 2,137 Nov-04-2020, 09:11 AM
Last Post: Sateesh
  Arrays in MATLAB and PYTHON cokhuatlanyeuthuongconhetmuc 2 2,953 Jul-24-2020, 10:47 AM
Last Post: cokhuatlanyeuthuongconhetmuc
  Matlab to Python -- Parallel Computing zistambo 1 2,786 Jun-10-2020, 04:59 PM
Last Post: pyzyx3qwerty
  Python v MatLab for graphs and plots CynthiaMoore 4 4,301 Apr-22-2020, 02:13 PM
Last Post: CynthiaMoore
  python equivalent to MATLAB xcov chai0404 2 4,883 Apr-02-2020, 10:29 PM
Last Post: chai0404
  Python equivalent of Matlab code kwokmaster 1 4,381 Mar-25-2020, 10:14 PM
Last Post: j.crater
  MATLAB to Python conversion stokd 10 6,804 Jan-19-2020, 09:14 PM
Last Post: stokd
  Help converting MATLAB triple-for loop to Python davlovsky 1 2,660 Oct-29-2019, 10:26 PM
Last Post: scidam
  Error -3 when opening matlab file in python python_newbie09 0 2,877 Aug-02-2019, 11:51 AM
Last Post: python_newbie09

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020