create structure array with eval

9 次查看(过去 30 天)
hi guys First of all I know that it is really inefficient to create structure array with eval.However i did not find a simple solution to create structure array for my evaluation tool. I am now programming kuka robot and for each position I obtain values and have to save my results in this array. Finally i would like to create main array which contains all positions and related results. However,Matlab saves just last Position (it means: Matlab overwrites) Main structure array=Ergebnisse How can solve this Problem? Could you please help me? I add following my codes:
eval(sprintf('Ergebnisse.P%d_%d_%d =[aus_fft] ',Position,PointId,Pose));
if i==it
auslenkung_ges=auswertung_2(auslenkung,i,choice);
eval(sprintf('Ergebnisse.auslenkung_Position_%d =[auslenkung_ges] ',Position));
end

采纳的回答

Stephen23
Stephen23 2016-9-19
编辑:Stephen23 2016-9-19
This is simple using dynamic fieldnames, and you do not need eval:
Ergebnisse.(sprintf('auslenkung_Position_%d',Position)) = ...
Dynamic fieldnames are explained clearly in the documentation:
And I notice that dynamic fieldnames been explained to you before in several of your earlier questions, but you keep on persisting in using the slowest and buggiest eval-based solutions... and then get stuck because your code is slow and buggy and hard to get working properly... maybe it would be a good time to understand why bad design decisions actually really do make your code worse.
Addendum: You might like to use a non-scalar structure, which would be much faster and simpler:
  2 个评论
cemsi888
cemsi888 2016-9-19
编辑:cemsi888 2016-9-19
it says invalid fieldname. Invalid field name: 'P1_1_30
Stephen23
Stephen23 2016-9-19
编辑:Stephen23 2016-9-19
Check how you generate the fieldname string:
>> Ergebnisse.('P1_1_30') = 3
Ergebnisse =
P1_1_30: 3
>> Ergebnisse.('''P1_1_30') = 3
Invalid field name: ''P1_1_30'.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Structures 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by