Data Structure Reference Assignment
이전 댓글 표시
When I implement the below it works fine for a single array variable:
Le2=length(GC);
FzGC2=nan(1000,Le2);%consider how to adjust ground contact lengths for different vector lengths during further eval
%Collect last step
for i=1:Le2 %or use N value
if ~isnan(GC(i,2))
tempFz2 = Fz(GC(i,1):GC(i,2)); %gets ground contact Fz data
length_tempFz2 = size(tempFz2,1); %find length of ground contact Fz data
FzGC2(1:length_tempFz2,i)= Fz(GC(i,1):GC(i,2)); %adjusts size of array to store it in
else
tempFz2 = Fz(GC(i,1):end); %gets ground contact Fz data
length_tempFz2 = size(tempFz2,1); %find length of ground contact Fz data
FzGC2(1:length_tempFz2,i)= Fz(GC(i,1):end); %adjusts size of array to store it in
end
end
However when I create a struture and try to do the same kind of reference I am getting an 'Index exceeds matrix dimensions' errror on the first bolded line and then the second bolded line if I remove the first while troubleshooting. GC is a 170*2 array with references ranging from 700 to 12000. Both Fz and CoPx are similar sizes - 1 structure with 6 fields (Trials 1-6). Trial sizes within the structure are 30000 or 120000 * 1
for q=1:length(filenames)
Q = size(CoPx.(fnc{q}),1);
%CoPx.(fnc{q})(isnan(fnc{q}))= 0;
for i=1:length(GC)
if ~isnan(GC(i,2))
tempCoPx = CoPx.(fnc{q})(GC(i,1):GC(i,2)); %gets ground contact
length_tempCoPx = size(tempCoPx,1); %find length of ground contact
CoPxGC(1:length_tempCoPx,i)= CoPx.(fnc{q})(GC(i,1):GC(i,2)); %adjusts size of array to store it in
else
tempCoPx = CoPx.(fnc{q})(GC(i,1):end); %gets ground contact Fz data
length_tempCoPx = size(tempCoPx,1); %find length of ground contact Fz data
CoPxGC(1:length_tempCoPx,i)= CoPx.(fnc{q})(GC(i,1):end); %adjusts size of array to store it in
end
CMean = mean(CoPxGC);
A=mean(CMean(1:2:end));%odd number contacts
B=mean(CMean(2:2:end));%even number contacts
%assuming +X is to the right
if A > B
RIGHT=1;
LEFT=2;
else
LEFT=1;
RIGHT=2;
end
end
%assign [left TD, left TO, right TD, right TO]
end
Can you please assist?
댓글 수: 5
A hint at first: Simplify your code. The first block can be written as:
Le2 = length(GC);
FzGC2 = nan(1000, Le2);
for i = 1:Le2 %or use N value
if isnan(GC(i,2))
cFz = Fz(GC(i,1):end);
else
cFz = Fz(GC(i,1):GC(i,2));
end
FzGC2(1:numel(cFz, i)) = cFz; % adjusts size of array to store it in
end
Of course, if you have GC(i,2) and GC(i,2) you can calculate the size of GC(i,2):GC(i,2) easily also:
Le2 = length(GC);
FzGC2 = nan(1000, Le2);
fixGC = GC;
fixGC(isnan(GC(:, 2))) = numel(Fz); % Care for the NaNs outside
for i = 1:Le2 % or use N value
a = fixGC(i, 1);
b = fixGC(i, 2);
FzGC2(1:b-a+1) = Fz(a:b); % adjusts size of array to store it in
end
This looks extremly cluttered:
tempCoPx = CoPx.(fnc{q})(GC(i,1):GC(i,2));
Your function contains to many information about the meaning of the data. A more abstract design would simplify the code. E.g. add this on top of the code:
X = CoPx.(fnc{q});
Then use X instead of the compicated expression. This make the code easier to read and it will even run faster.
If the code is cleaner, it gets much easier to find the cause, why CoPx.(fnc{q}) does not have the size you expect. The posted code does not contain the definition of this variable, so the readers cannot find out, where the problem is.
Ruud
2021년 9월 8일
Ruud
2021년 9월 9일
Jan
2021년 9월 9일
CoPx.(strcat('Trial_',num2str(q)))
This is hiding the index of an array in the field name. Use arrays with inidices is easier than this indirection.
This looks complicated: Fz.(fn{q})(n:L) < threshold . You can move it out of the loop:
hasFz = (Fz.(fn{q})(n:L) >= threshold);
Now the change point can be found by:
padFz = [false, hasFz, false];
touchDown = strfind(padFz, [false, true]);
liftOff = strfind(padFz, [true, false]);
By this way, you do not need a loop.
Ruud
2021년 9월 9일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!