'Reference to non-existent field' when using nested for loops
Show older comments
Hi,
I'm trying to calculate some simpe things for data that is in structures and put it in a new 'outcome' structure which can later be exported to excel.
I have the following code:
def = {'Acc_X','Acc_Y','Acc_Z','Gyr_X','Gyr_Y','Gyr_Z'};
RMSdef = {'RMS_Acc_X','RMS_Acc_Y','RMS_Acc_Z','RMS_Gyr_X','RMS_Gyr_Y','RMS_Gyr_Z'};
for k = 1:size(sensors,2)
for j = 1: length(def)
for u = 1:nEVENTS/2
% Max values & timing of maximal values
[OUTCOME(k).event(u).(MAXdef{j}),OUTCOME(k).event(u).(idxMAXdef{j})] = max(abs(Cycle(k).event(u).(def{j})));
% RMS
OUTCOME(k).event(u).(RMSdef{j}) = rms(Cycle(k).event(u).(def{j}));
% Sample entropy
OUTCOME(k).event(u).(E{j}) = SampEn(2,0.2*std(Cycle(k).event(u).(def{j})),(Cycle(k).event(u).(def{j})));
end
end
end
This works perfectly.
Now I want to do something similar:
jerkdef = {'Jerk_X','Jerk_Y','Jerk_Z'};
accdef = {'Acc_X','Acc_Y','Acc_Z'};
for k = 1:size(sensors,2)
for q = 1: length(accdef)
for u = 1:nEVENTS/2
% Calculate jerk
JERK(k).event(u).(jerkdef{q}) = diff(Cycle(k).event(u).(accdef{q}));
% Norm of jerk
JERK(k).event(u).NormJerk = ...
sqrt(((JERK(k).event(u).Jerk_X).^2 + (JERK(k).event(u).Jerk_Y).^2 + (JERK(k).event(u).Jerk_Z).^2));
end
end
end
For some reason, matlab gives me "Reference to non-existent field 'Jerk_Y' ", but when I run this line seperately:
JERK(1).event(1).(jerkdef{2}) = diff(Cycle(1).event(1).(accdef{2}));
It does work.
I might be missing something very very very obvious but currently, I'm lost..
Thanks in advance
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements 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!