Many functions in a large script file

1 回表示 (過去 30 日間)
sadi tavakoli
sadi tavakoli 2019 年 6 月 20 日
コメント済み: Walter Roberson 2019 年 6 月 26 日
I am wondered how can I add nested functions in a parent function in a while loop. Is that possible?
At the moment, the script file works with calling functions, but I need to compile the script code. Any idea?

採用された回答

Walter Roberson
Walter Roberson 2019 年 6 月 20 日
"I am wondered how can I add nested functions in a parent function in a while loop. Is that possible?"
No. functions cannot be defined inside of control structures.
"At the moment, the script file works with calling functions, but I need to compile the script code."
Add a function header at the top of the file, and add an "end" statement before the first "function" that you have defined in the file.
  2 件のコメント
sadi tavakoli
sadi tavakoli 2019 年 6 月 20 日
Thanks for the comment.
So, there is no way to compile my code in MATLAB!
What about Simulink? Does not it help for compiling such a functions in function code?
Steven Lord
Steven Lord 2019 年 6 月 20 日
No, as Walter said there is no way to define a nested function inside a while loop.
Why do you believe you need to do that? Why do you believe nesting the function before or after the while loop is not an option? If you're trying to use the nested function inside the loop and believe that means you need to define it inside the loop, that's not the case. Yes, I know this is one of the least efficient ways to create this vector, but it was quick to write.
function x = nestedFunctionExample
n = 10;
x = [];
while n > 0
callNestedFunction;
end
function callNestedFunction
x = [x n];
n = n-1;
end
end
This same example would have worked identically if I had defined it:
function x = nestedFunctionExample
n = 10;
x = [];
function callNestedFunction
x = [x n];
n = n-1;
end
while n > 0
callNestedFunction;
end
end
What you can't do is this:
function x = nestedFunctionBADExample
n = 10;
x = [];
while n > 0
function callNestedFunction
x = [x n];
n = n-1;
end
callNestedFunction;
end
end

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

その他の回答 (1 件)

sadi tavakoli
sadi tavakoli 2019 年 6 月 25 日
Thanks Steven and Walter. It works now properly.
In addition, I also stucked in the next step when I am trying to compile my function to a .dll file. This function(s) loads a thermodynamic file from Cantera (*.cti), and a command named 'Solution':
gas = Solution('filename.cti);
Running the function gives the results without any error, but in compiling, I encounter this error: 'No class Solution '
Does it mean MATLAB does not support Cantera in the compiling process?
  1 件のコメント
Walter Roberson
Walter Roberson 2019 年 6 月 26 日
It sounds as if for some reason Solution.m or the @Solution directory is not being added to the project files.

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

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by