When using assignin in subfunction A to create a variable in the workspace of subfunction B, the variable is being overwritten by a MATLAB built-in function with the same name
Show older comments
Here is a similar example code. In the original code, there are numerous variables, making it difficult to refactor the code structure or rename variables. I aim to understand the root cause of the error. Specifically, in the example code, even though a variable beta exists in the workspace of subfunction test_beta, MATLAB still recognizes beta as the built-in function. Notably, this issue does not occur when the variable is assigned in the base workspace of the main program.
output = test_beta(10);
function output = test_beta(input)
test_beta2(input)
output = beta;
end
function test_beta2( input )
assignin('caller', 'beta', input);
end
1 Comment
"I aim to understand the root cause of the error."
See 3) here:
and in the documentation here:
Solution: write better code that does not make variables magically appear without any warning.
Accepted Answer
More Answers (0)
Categories
Find more on Programming 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!