Transferring variables between scripts and functions

8 次查看(过去 30 天)
Hi, I have a matlab script(say test.m) which calculates a quantity(say a).Now i want to write a function(compute.m) which takes the variable a from test.m and calculate more quantities(say F). I want to call compute.m in test.m after this. Is it possible to do so?Please help. Also, how can I get two matlab scripts to share the same variable.I am aware of global variables but they work out only for functions as far as I know.Thanks!

回答(1 个)

Walter Roberson
Walter Roberson 2013-3-7
"global" works in scripts as well. On the other hand, scripts use the current workspace instead of creating their own, so if you have one script call another, or run two scripts after each other, then they will have access to all of each other's variables.
To have the function be able to access a variable from the script that called it, you can use evalin('caller'). This is not recommended, though: it is better for the value of the variable to be passed in the call to the function.

类别

Help CenterFile Exchange 中查找有关 Entering Commands 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by