How do I use same variables in different scripts?
32 次查看(过去 30 天)
显示 更早的评论
Hello,
Sorry for the weird English. I did my best to explain what I am struggling with....
I am wondering if you assign variable in one script, then can use those variables in another script?
For example,
In script A,
A=12;
B=23;
C=25;
D=...;
C=....;
there are tons of variables in script A.
In function script,
I want to use all variables in script A instead of assigning everything again.
thanks,
0 个评论
采纳的回答
Adam Danz
2021-3-28
编辑:Adam Danz
2021-3-28
If you run script A and then script B in the same workspace, then all variables in A will remain available unless a variable is explicitly cleared or, more commonly, overwritten. This is poor workflow, though. It becomes quite difficult to troubleshoot the origin of variables and how they were changed.
A better idea is to use function input/outputs. If you're working with a lot of variables, you can store them in a structure or cell array to reduce the number of inputs/outputs and to keep your data organized.
Don't use global variable, eval, evalin, etc, to pass variables.
For other opinions on this, see these threads in the forum
2 个评论
Adam Danz
2021-3-28
Agreed. I know it may seem like a lot of work to change tons of variables but now you know how to plan for this in the future and your updated functions will work so much more smoothly.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!