how to share multiple variables in different m files?
8 次查看(过去 30 天)
显示 更早的评论
Now i have two solutions to address the problem.
- The one is set all the variables need to share to ouptput variable,and input them when it's used in other function.
- The other is set all these variables to global variable.Does Matlab can address it by other better solution?
0 个评论
采纳的回答
Star Strider
2014-8-29
If your m-files are scripts running independently of each other, my choice would be to use a .mat file. Save all the variables in a .mat file and then using load with an output argument, load only the variables needed by a particular script into the workspace.
If your m-files are all functions running in your workspace at the same time, you can pass the variables as arguments as needed by the functions and then return new values for them as function outputs (or use the much less-desirable option of declaring them as global variables).
It all depends on what your m-files are doing, and whether they need the variables serially or simultaneously.
0 个评论
更多回答(1 个)
Jasper
2014-8-29
What type of program are you using this in? Are you using some form of parallel processing?
If not, I would recommend passing variables as a structure to the functions, and returning a similar (updated) structure as the output of the functions.
For instance:
function output=fcn(input)
output=input;
output.var1=input.var1*20;
end
p.var1=2^8;
p.var2='somestring';
p=fcn1(p);
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Workspace Variables and MAT-Files 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!