How do I get nested loops in a function?

2 次查看(过去 30 天)
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