create structure array with eval

9 views (last 30 days)
cemsi888
cemsi888 on 19 Sep 2016
Edited: Stephen23 on 19 Sep 2016
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

Accepted Answer

Stephen23
Stephen23 on 19 Sep 2016
Edited: Stephen23 on 19 Sep 2016
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 Comments
cemsi888
cemsi888 on 19 Sep 2016
Edited: cemsi888 on 19 Sep 2016
it says invalid fieldname. Invalid field name: 'P1_1_30
Stephen23
Stephen23 on 19 Sep 2016
Edited: Stephen23 on 19 Sep 2016
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'.

Sign in to comment.

More Answers (0)

Categories

Find more on Structures in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!