Why doesn't MATLAB recognize the variable I have assigned in a script called from a function?
古いコメントを表示
In a function, I call a script that assigns various variables, one of which (tf) happens to be the name of a predefined MATLAB function. When I then evaluate the variable in the function, it defaults as the MATLAB function name rather than the variable.
Is there a good reason for this? Is there an optimal solution, other than changing my variable name?
2 件のコメント
"Is there an optimal solution"
Yes: change the variable name.
I know you will then point out that you specifically asked for a solution other than this... but the reality is that this is the best solution. Naming every function/script/variable with its own unique name means that you will never face any uncertainty in what will be called, that any error message will unambiguously relate to that function/script/variable, that changing the MATLAB Search Path does not run the risk of changing your code's behavior, that you can distribute your code to others and know that it will work, that you make it clear in your code where that script/function/variable is called that you really mean my_special_tf and not just tf (which to any reasonable person using an internet search engine would mean an inbuilt MATLAB function)... oh, and of course it also means that you can still use the inbuilt function!
Walter Roberson
2018 年 6 月 16 日
The problem is the variable name, not the script name.
採用された回答
その他の回答 (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!