フィルターのクリア

How to define a nested function inside a for loop?

10 ビュー (過去 30 日間)
vikas mishra
vikas mishra 2017 年 12 月 1 日
コメント済み: vikas mishra 2017 年 12 月 1 日
I wish to define a nested function inside a for loop. When I did it I encountered an error. When I further inquired I found the following:
``You cannot define a nested function inside any of the MATLAB® program control statements, such as if/elseif/else, switch/case, for, while, or try/catch."
which is available at
https://www.mathworks.com/help/matlab/matlab_prog/nested-functions.html#btfml3d
Any by pass to this? Can I define a nested function inside a loop?
Thank you.
  4 件のコメント
Matt J
Matt J 2017 年 12 月 1 日
Your attached files contain neither loops nor nested functions. They therefore cannot clarify where you are trying to place a nested function, nor why you think it needs to go inside a loop.
vikas mishra
vikas mishra 2017 年 12 月 1 日
@ Matt J, Thanks. Yes, you are correct that attached files contain neither loops nor nested functions. As I already wrote that I want to put my theta_0 in the for loop form 0 to 21, so that I do not need to update it manually. You van see that theta_0 is in two files, so I made a single file with nested functions. However, I have to put one nested function inside a for loop. This is the difficulty. If you have any other way to do it please let me know. For your kind perusal, I have also attached the updated file with nested functions.
In short, my question is how to put a for loop for theta_0 varying between 0 to 21.
Thanks

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

回答 (1 件)

Matt J
Matt J 2017 年 12 月 1 日
編集済み: Matt J 2017 年 12 月 1 日
There should be no need to place a nested function definition inside a loop. My advice would be to simply move it outside the loop, placing it somewhere else in the same workspace. As long as the nested function resides in the same workspace as the loop, they should both have access to all variables in that workspace.

カテゴリ

Help Center および File ExchangeDebugging and Analysis についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by