Using Structure Array in Loop

조회 수: 15 (최근 30일)
Justine Pearce
Justine Pearce 2018년 12월 28일
편집: Matt J 2018년 12월 28일
Hi, I am trying to create a for loop using data points stored within a structure array. I can't workout how I would do this. I'm new to coding so I tried the following but am getting errors regardless of what I try. Currently my first for loop runs providing me with WINDW1 (which is essentially just the sample rate). I then need to run the loop using each individual sample rate and its corresponding ODBA to calculate ODBA_1sec but it is here I get stuck as I can't workout how to get the loop to sync the right sample rate with its ODBA within the equation.
This is the code which works if I run it individually using my structure array - I change the (3) depending on which sample frequency I want to run with.
%% Smooth the data for activity measures
% Sample rate used change (?) number to indiciate sample frequency 1 = 400; 2=200; 3=100; 4= 50; 5=25
sample_Hz = c_accData(3).sampleRate; %Change to the correct sample rate used
% Calculate the mean ODBA at 1 second intervals.
WINDW1 = 1*sample_Hz;
ODBA_1sec = accumarray(ceil((1:numel(c_accData(3).ODBA))/WINDW1)',c_accData(3).ODBA(:),[],@mean);
% Display message at the end of this section
disp('Data has been smoothed');
I then tried to make this into a for loop by bringing the structure array into one variable name which I could use within the equations. Matlab errors include
"Error using horzcat
Dimensions of matrices being
concatenated are not
consistent."
and that
"Variavle 'ODBA_Hz' might be set by a non-scalar operator."
sample_Hz = [c_accData(1).sampleRate c_accData(2).sampleRate c_accData(3).sampleRate c_accData(4).sampleRate c_accData(5).sampleRate];
for j = 1:sample_Hz
WINDW1 = 1*sample_Hz;
end
ODBA_Hz = [c_accData(1).ODBA c_accData(2).ODBA c_accData(3).ODBA c_accData(4).ODBA c_accData(5).ODBA];
for k = 1:ODBA_Hz
ODBA_1sec = accumarray(ceil((1:numel(ODBA_Hz))/WINDW1)',ODBA_Hz(:),[],@mean);
disp('Data has been smoothed');
end
I'm sure it's a simple task just not one I have learnt yet so I hope someone can help. Thanks in advance.
Justine
  댓글 수: 3
Justine Pearce
Justine Pearce 2018년 12월 28일
Hi, okay, that makes sense. Do you have any tips on making it work?
Yes, it's because that message wasn't an actual error message it comes up when I hover over a red dash on the right suggesting an issue with my code so I couldn't copy it. Sorry for the typo.
Matt J
Matt J 2018년 12월 28일
편집: Matt J 2018년 12월 28일
Do you have any tips on making it work?
Replace ODBA_Hz with the intended scalar value, whatever that is,
for k = 1:kmax

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

답변 (0개)

카테고리

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

제품


릴리스

R2017a

Community Treasure Hunt

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

Start Hunting!

Translated by