Structure For loop: moving sections of one structure to another structure with indexing variables.

조회 수: 2 (최근 30일)
%create cells that are looped through to access each structure area in for
%loop
StartTimes = [SitStand1Start SitStand2Start SitStand3Start NormalWalk1Start NormalWalk2Start NormalWalk3Start SlowWalk1Start SlowWalk2Start SlowWalk3Start FastWalk1Start FastWalk2Start FastWalk3Start Running1Start Running2Start Running3Start];
EndTimes = [SitStand1End SitStand2End SitStand3End NormalWalk1End NormalWalk2End NormalWalk3End SlowWalk1End SlowWalk2End SlowWalk3End FastWalk1End FastWalk2End FastWalk3End Running1End Running2End Running3End];
Data = {'a' 'w' 'm' 'q'}
Segments = {'LTh' 'RTh' 'RShank' 'LShank' 'RFoot' 'LFoot' 'Th'}
Trials = {'SitStand1' 'SitStand2' 'SitStand3' 'NormalWalk1' 'NormalWalk2' 'NormalWalk3' 'SlowWalk1' 'SlowWalk2' 'SlowWalk3' 'FastWalk1' 'FastWalk2' 'FastWalk3' 'Run1' 'Run2' 'Run3'}
%Create the new structure that will organize the data by trial instead of sensor.
D = struct('a',[],'w',[],'m',[],'q',[]);
S = struct('LTh', D, 'RTh', D, 'RShank' ,D, 'LShank',D, 'RFoot', D, 'LFoot',D,'Th',D);
Col = struct('SitStand1',S,'SitStand2',S,'SitStand3',S,'NormalWalk1',S,'NormalWalk2',S,'NormalWalk3',S,'SlowWalk1',S,'SlowWalk2',S,'SlowWalk3',S,'FastWalk1',S,'FastWalk2',S,'FastWalk3',S,'Run1',S,'Run2',S,'Run3',S)
clear D S
for Seg = 1:length(Segments)
for Tri = 1:length(Trials)
Col.Trials(Tri).Segments(Seg).a = IMU.Segments{Seg}.a(StartTimes(Tri):EndTimes(Tri),:);
Col.Trials(Tri).Segments(Seg).w = IMU.Segments(Seg).w(StartTimes(Tri):EndTimes(Tri),:);
Col.Trials(Tri).Segments(Seg).m = IMU.Segments(Seg).m(StartTimes(Tri):EndTimes(Tri),:);
Col.Trials(Tri).Segments(Seg).q = IMU.Segments(Seg).q(StartTimes(Tri):EndTimes(Tri),:);
end
end
In the above code I am trying to index through the original structure (IMU) that was given to me and create a processing structure (Col) that breaks the data down into trials that were collected during the complete data collection.
I know this isnt the most optimized process but it should do what I need it to do. What I'm having trouble with is the following error.
Reference to non-existent field 'Segments'.
Col.Trials(Tri).Segments{Seg}.a = IMU.Segments{Seg}.a(StartTimes(Tri):EndTimes(Tri),:);
Which doesn't make sense to me as the field does exist as I already created the structure and when I access the field in the command window I am not getting that error. I think this might be a syntex detail that I'm missing but advice is greatly appreciated.
  댓글 수: 1
Voss
Voss 2024년 4월 11일
You can avoid having to copy/paste or type two times all those field names, e.g.:
D = struct('a',[],'w',[],'m',[],'q',[]);
S = struct('LTh', D, 'RTh', D, 'RShank' ,D, 'LShank',D, 'RFoot', D, 'LFoot',D,'Th',D);
Col = struct('SitStand1',S,'SitStand2',S,'SitStand3',S,'NormalWalk1',S,'NormalWalk2',S,'NormalWalk3',S,'SlowWalk1',S,'SlowWalk2',S,'SlowWalk3',S,'FastWalk1',S,'FastWalk2',S,'FastWalk3',S,'Run1',S,'Run2',S,'Run3',S);
The field names are already stored in the variables Data, Segments, and Trials, and you can re-use those to construct D, S, and Col:
Data = {'a' 'w' 'm' 'q'};
Segments = {'LTh' 'RTh' 'RShank' 'LShank' 'RFoot' 'LFoot' 'Th'};
Trials = {'SitStand1' 'SitStand2' 'SitStand3' 'NormalWalk1' 'NormalWalk2' 'NormalWalk3' 'SlowWalk1' 'SlowWalk2' 'SlowWalk3' 'FastWalk1' 'FastWalk2' 'FastWalk3' 'Run1' 'Run2' 'Run3'};
temp = Data;
temp(end+1,:) = {[]};
D = struct(temp{:})
D = struct with fields:
a: [] w: [] m: [] q: []
temp = Segments;
temp(end+1,:) = {D};
S = struct(temp{:})
S = struct with fields:
LTh: [1x1 struct] RTh: [1x1 struct] RShank: [1x1 struct] LShank: [1x1 struct] RFoot: [1x1 struct] LFoot: [1x1 struct] Th: [1x1 struct]
temp = Trials;
temp(end+1,:) = {S};
Col = struct(temp{:})
Col = struct with fields:
SitStand1: [1x1 struct] SitStand2: [1x1 struct] SitStand3: [1x1 struct] NormalWalk1: [1x1 struct] NormalWalk2: [1x1 struct] NormalWalk3: [1x1 struct] SlowWalk1: [1x1 struct] SlowWalk2: [1x1 struct] SlowWalk3: [1x1 struct] FastWalk1: [1x1 struct] FastWalk2: [1x1 struct] FastWalk3: [1x1 struct] Run1: [1x1 struct] Run2: [1x1 struct] Run3: [1x1 struct]

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2024년 4월 11일
for Seg = 1:length(Segments)
for Tri = 1:length(Trials)
Col.(Trials{Tri}).(Segments{Seg}).a = IMU.(Segments{Seg}).a(StartTimes(Tri):EndTimes(Tri),:);
Col.(Trials{Tri}).(Segments{Seg}).w = IMU.(Segments{Seg}).w(StartTimes(Tri):EndTimes(Tri),:);
Col.(Trials{Tri}).(Segments{Seg}).m = IMU.(Segments{Seg}).m(StartTimes(Tri):EndTimes(Tri),:);
Col.(Trials{Tri}).(Segments{Seg}).q = IMU.(Segments{Seg}).q(StartTimes(Tri):EndTimes(Tri),:);
end
end
  댓글 수: 3
Sydney Lundell
Sydney Lundell 2024년 4월 11일
Thank you! I figured it had something to do with () and {} to call the fields. The other suggestions for optimizing the code is appreciated.
Voss
Voss 2024년 4월 11일
편집: Voss 2024년 4월 11일
You're welcome!
To briefly explain, for instance, why IMU.(Segments{Seg}) works and IMU.Segments(Seg) and IMU.Segments{Seg} do not:
IMU.Segments(Seg) and IMU.Segments{Seg} refer to a field literally called "Segments" in the structure IMU, which doesn't exist, hence the error you got.
On the other hand IMU.(Segments{Seg}) refers to a field called whatever the value of Segments{Seg} is, which in this case is the actual name of the field you want, since Segments is a cell array containing those field names, and Segment{Seg} is one such field name.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by