Creating structure using eval function

6 vues (au cours des 30 derniers jours)
Thulasi Durai Durai Samy
Thulasi Durai Durai Samy le 6 Juil 2012
hello I need to create structure using eval function
required structure
laminate =
var1 = [0 0 0 0 0 0 0 0 0]
where
value = zeors(3);
evalin('base',['Laminate','=struct(''',''',var1,'''',''',value,''')'])
matlab error
Error using ==> horzcat CAT arguments dimensions are not consistent.
but for var1 = 'any string' is accepted
any idea to solve this problem.

Réponse acceptée

Jan
Jan le 6 Juil 2012
No, I'm really convinced that you do not need to create this using EVAL (or EVALIN as in your example). There is always a better solution than the evil EVAL. Do trust the FAQ and rely on hundrets or related threads in this forum, most of all consider that this method does produce problems for you already.
Therefore it is safer, simpler and more efficient to create the variable directly:
function Output = myFunc
Output.var1 = zeros(1, 6);
Now calling Laminate = myFunc from the command line create the wanted struct without dirty tricks.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by