Creating structure using eval function

6 views (last 30 days)
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.

Accepted Answer

Jan
Jan on 6 Jul 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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!