MATLAB gives error while running a good Python code: TypeError: 'float' object is not iterable

6 ビュー (過去 30 日間)
I am trying to run an averaging code that requires a list and a single number tau. The python code runs well in Python 3.5.3 with inputs for eg. average([[12,13,14],[24,26,28]],0.5). The python code is as follows:
from math import *
def average(spike_trains,tau):
class Delta_error:
def __init__(self,spike_trains,tau):
self.n=len(spike_trains)
self.big_train=[]
for train in spike_trains:
self.big_train=self.big_train + train
#print(self.big_train)
#self.big_train.append(train)
self.centre_train=[]
self.tau=tau
self.min=min(self.big_train)
self.max=max(self.big_train)
def add_spike(self,time):
self.centre_train.append(time)
def minimize(self):
value=1.0
time=self.min
for spike in self.big_train:
new_value=self.__call__(spike)
if new_value<value:
value=new_value
time=spike
return time
def sort(self):
self.centre_train.sort()
delta_error=Delta_error(spike_trains,tau)
train_length=len(delta_error.big_train)//len(spike_trains)
for spike_c in range(0,train_length):
new_spike = delta_error.minimize()
delta_error.add_spike(new_spike)
delta_error.sort()
return delta_error.centre_train
Answer: [13, 24, 26]. In python.
When running in MATLAB running Python 3.5.3 (from pyversion)
py.average.average([[12,13,14],[24,26,28]],0.5)
I get the following vague error:
Python Error: TypeError: can only concatenate list (not "float") to list
The bolded lines have been problematic in MATLAB, but after changing the version of python running in MATLAB, I am only getting a one line error.
I need this code to work in MATLAB. What should I do? I would appreciate all tips and suggestions!
  2 件のコメント
Kojiro Saito
Kojiro Saito 2019 年 8 月 7 日
Could you show us "__call__" definition in your Python scripts?
Ritwika Mukherjee
Ritwika Mukherjee 2019 年 8 月 7 日
This is not problematic so far. Here it is:
def __call__(self,time):
cross1=0.0
for centre_time in self.centre_train:
cross1+=exp(-fabs(centre_time-time)/self.tau)
cross2=0.0
for big_time in self.big_train:
cross2+=exp(-fabs(big_time-time)/self.tau)
return 1.0+2.0*cross1-2.0/self.n*cross2

サインインしてコメントする。

採用された回答

Kojiro Saito
Kojiro Saito 2019 年 8 月 7 日
MATLAB needs to pass Python list and tuple data to your python class.
The followings work fine.
py.average.average(py.list({py.list({12,13,14}), py.list({24,26,28})}),0.5)
py.average.average(py.list({py.list([12,13,14]), py.list([24,26,28])}),0.5)
  2 件のコメント
Ritwika Mukherjee
Ritwika Mukherjee 2019 年 8 月 8 日
This worked! Thank you!
I was trying to use pylist before when I was trying to use a separate command structure:
EMGlist= py.list({py.list(m1.Ch2raster'),py.list(m2.Ch2raster')}); %that's my data in MATLAB
systemCommand=['python.exe average.py', EMGlist, 0.5] %python address, python code, %input arguments
system(sytemCommand)
This didn't work. I am not fully sure when py.list is needed and when it is not. Would you know why that is?
Again, thank you so much for fixing this!!!
Kojiro Saito
Kojiro Saito 2019 年 8 月 9 日
Here is an example of py.list for calling Python function from system command.
EMGlist= py.list({py.list([12,13,14]), py.list([24,26,28])});
systemCommand=['python.exe .\average.py ', '"', char(EMGlist), '" ', num2str(0.5)]; %python address, python code, %input arguments
[status,cmdout] = system(systemCommand)
Because system command (command line) allows characters but does not allow py.list, we need to convert py.list to characters and enclose those characters with single or double quotations.
Also, you need to add following codes in your python function in order to allow arguments from command line. This is a sample and might not be good-looking.
import sys
import ast
def average(spike_trains,tau):
# This part is same
#
if __name__ == '__main__':
spike_trains = ast.literal_eval(sys.argv[1])
tau = float(sys.argv[2])
print(average(spike_trains, tau))

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by