MATLAB gives error while running a good Python code: TypeError: 'float' object is not iterable
7 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Ritwika Mukherjee
il 6 Ago 2019
Commentato: Kojiro Saito
il 9 Ago 2019
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 Commenti
Risposta accettata
Kojiro Saito
il 7 Ago 2019
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 Commenti
Kojiro Saito
il 9 Ago 2019
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))
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Call Python from MATLAB in Help Center e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!