필터 지우기
필터 지우기

create structure array with eval

조회 수: 8 (최근 30일)
cemsi888
cemsi888 2016년 9월 19일
편집: Stephen23 2016년 9월 19일
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