How do I get nested loops in a function?

1 Ansicht (letzte 30 Tage)
Inti Vanmechelen
Inti Vanmechelen am 19 Dez. 2015
Kommentiert: Inti Vanmechelen am 20 Dez. 2015
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!

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Dez. 2015
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.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by