How do I use same variables in different scripts?

조회 수: 52 (최근 30일)
Duckyoon Go
Duckyoon Go 2021년 3월 28일
댓글: Adam Danz 2021년 3월 28일
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,

채택된 답변

Adam Danz
Adam Danz 2021년 3월 28일
편집: Adam Danz 2021년 3월 28일
Review Scripts vs Function and Base and Function Workspaces from the documentation.
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
Duckyoon Go
Duckyoon Go 2021년 3월 28일
Thank you sir, I guess my approach is not appropriate. Links helped alot.
Adam Danz
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 CenterFile Exchange에서 Creating, Deleting, and Querying Graphics Objects에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by