Why doesn't MATLAB recognize the variable I have assigned in a script called from a function?
Show older comments
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 Comments
"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
on 16 Jun 2018
The problem is the variable name, not the script name.
Accepted Answer
More Answers (0)
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!