How do I get nested loops in a function?

조회 수: 1 (최근 30일)
Inti Vanmechelen
Inti Vanmechelen 2015년 12월 19일
댓글: Inti Vanmechelen 2015년 12월 20일
I'm trying to replace my for loops by functions to get a more efficient code, but I can't succeed to get nested loops in to a function.
For example for this part of the code:
for i = 1:10;
EMGMax.(subjects{i}) = max(EMGMaxMotion.(subjects{i}));
for k = 1:3;
EMGNor.(subjects{i}).(motion{k}) = SmoothSignal.(subjects{i}).(motion{k})(:,2:21)/(EMGMax.(subjects{i}));
TimePoints = size(SmoothSignal.(subjects{i}).(motion{k}),1);
for t = 1:TimePoints
EMGNor.(subjects{i}).(motion{k})(t,:) = SmoothSignal.(subjects{i}).(motion{k})(t,2:21)./(EMGMax (subjects{i}));
end
end
end
I succeeded to replace the first for loop by the function:
[EMGMax] = findEMGMax(EMGMaxMotion,subjects);
But if I try to replace the second for loop by this function:
function[EMGNor] = findEMGNor(SmoothSignal,subjects,motion,EMGMax)
Matlab says "subscript must be real positive integers of logicals", and I can't figure out how to solve this error.
I hope my question is clear!

채택된 답변

Walter Roberson
Walter Roberson 2015년 12월 19일
When you moved the code into a function, does the function still refer to i ? If so then the i would not have a definition, and would default to being sqrt(-1) which is not a valid subscript.

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by