I'm making a soft that call matlab functions, and i want to pass a dict as argument in a matlab function using matlab.engine in python.
Like this:
def Parametrize(confFile):
""" Return
Argument:
confFile -- str() Configuration File path
Function that call MatLab function passing handles structure/dict as
argument. The return value of MatLab function called Parametrize(handles)
is the modified handles structure.
"""
print("ouai")
test = dict()
keys = range(4)
values = ["Hi", "I", "am", "John"]
for i in keys:
test[str(i)] = values[i]
eng = matlab.engine.start_matlab()
eng.addpath(vssFolderPath)
res = eng.Transit(test)
print(type(res))
print(res)
And the matlab function is pretty basic i'm testing how to pass data from Python to Matlab:
function a = Transit(test)
field = 'Value1';
value = {'TEST'};
disp(test)
a = struct(field,value);
i always have this error :
ValueError: invalid field for MATLAB struct
But i read this document that explain how to pass data from python to matlab and i don't know why it's not working for me.
The documentation says:
That is passing a dictionary to MATLAB is not supported if your keys are numeric. I think this would also apply to numbers converted to strings. From your code it looks like your dictionary is:
test={'0':'Hi','1':'I','2':'am','3':'John'}
Here the keys, although strings, are numbers [0-9]
.
Field-names in MATLAB cannot start with a numeric character [0-9]
(matlab.lang.makeValidName
). Try changing the keys to start with an alphabetic character [a-zA-Z]
. In your case, it is seems that MATLAB is trying to create field-names from your keys and failing because the field-names start with a number which MATLAB does not support as per the docs.