How do I get nested loops in a function?

2 vues (au cours des 30 derniers jours)
Inti Vanmechelen
Inti Vanmechelen le 19 Déc 2015
Commenté : Inti Vanmechelen le 20 Déc 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!

Réponse acceptée

Walter Roberson
Walter Roberson le 19 Déc 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.
  1 commentaire
Inti Vanmechelen
Inti Vanmechelen le 20 Déc 2015
That was it, thank you!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by