Structure For loop: moving sections of one structure to another structure with indexing variables.
3 ビュー (過去 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
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{:})
temp = Segments;
temp(end+1,:) = {D};
S = struct(temp{:})
temp = Trials;
temp(end+1,:) = {S};
Col = struct(temp{:})
採用された回答
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 件のコメント
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 Center および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!