How can I define a function within a loop?
古いコメントを表示
Say I want to repeat this user defined function within a loop with , how can I do it? Each time I do it like below I get this message - "Function definition is misplaced or improperly nested."
r=1;
while(1)
function y= myFunc (x)
y= -(500000*r*x(1));
r=r+1;
end
if (r>10)
break;
end
end
3 件のコメント
John D'Errico
2017 年 3 月 21 日
You just asked this same question, answered. What was wrong with the last answer, where I showed how to create a similar function in a loop?
Tanvir Kaisar
2017 年 3 月 21 日
Adam
2017 年 3 月 21 日
You seem to be misunderstanding the fundamental difference between defining a function and calling it. You define it once and can call it as many times as you like. I'm sure this is covered in the documentation on functions though to be honest I've never needed to look since I know what a function is and how it works.
採用された回答
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!