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.
  1 件のコメント
Inti Vanmechelen
Inti Vanmechelen 2015 年 12 月 20 日
That was it, thank you!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by