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
2 views (last 30 days)
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
Accepted Answer
Matt J
on 22 Mar 2025
Edited: Matt J
on 22 Mar 2025
The real solution is this,
function output = test_beta(input)
output = test_beta2(input);
end
function beta=test_beta2( input )
beta=input;
end
but if you must use assignin, you can get around the issue by doing,
output = test_beta(10)
function output = test_beta(input)
beta=[];
test_beta2(input)
output = beta;
end
function test_beta2( input )
assignin('caller', 'beta', input);
end
0 Comments
More Answers (0)
See Also
Categories
Find more on Startup and Shutdown 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!