How to access input variable in base workspace?
1 次查看(过去 30 天)
显示 更早的评论
Hi I want to write a function with one input parameter and no output parameters. How do I access the input parameter in its base worksapce in order to manipulate it?
1 个评论
采纳的回答
Fangjun Jiang
2011-8-11
You have to start with the base workspace. For example, you have par=1 in base workspace. Then you call your function Process(par).
Your function might look like:
function Process(Var)
VarName=inputname(1);
assignin('base',VarName,Var+rand);
However, this is not recommended because you are manipulate variables across boundary or scope. You can always do it this way, assume initially par=1 in base workspace
par=Process(par);
Where your Process() function looks like:
function out=Process(par)
out=par+rand;
7 个评论
Fangjun Jiang
2011-8-12
@Stagleton, that could work. But you could also do as below if you define the output argument of your function. That is more safer and follow a good programming practice regarding variable scope.
r=1;
r=inc(r);
更多回答(0 个)
另请参阅
类别
在 Help Center 和 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!