passing lots of variables between function efficiently?
显示 更早的评论
Hi
I have a question here.
I want to passing lots of variables between different function.
I defined those variables in a m file. And my other m files (functions) need use those variables.
How can I efficiently share those variables?
I know I can define a structure to put all the variables.
But every time I need use those variables I need write down lots of line at the beginning such as
nelx = Prob.nelx;
nely = Prob.nely;
...
...
...
I was wondering is there any other ways to do that?
采纳的回答
更多回答(3 个)
Thorsten
2013-2-11
Instead of writing statements like
nelx = Prob.nelx;
nely = Prob.nely;
You can do your computations on Prob.nelx and Prob.nely, i.e, in your code you simply prefix each nelx, nely, ... by a 'Prob.', e.g,
x = 123*Prob.nelx + 2*Prob.nely;
Youssef Khmou
2013-2-11
0 个投票
You create your variables in the workspace and save them with "save command" , per example if you create two variables a and b, then : save youfilename a b
in different M-files you load the file with "load" function :
load yourfilename
5 个评论
Kai
2013-2-11
Youssef Khmou
2013-2-11
i do not think so, anyway i proposed a way out, use tic toc commands with load and without load and conclude .
Walter Roberson
2013-2-11
Going to disk is always going to be slower than staying in memory.
Youssef Khmou
2013-2-11
编辑:Youssef Khmou
2013-2-11
ok then put all your functions in one signle M-file and make your variables global .
>>doc global
Jan
2013-2-11
Writing to disk will definitely a bottleneck in your program. Global variables are a bad idea also: Even if this would accelerate the program by some percent, the debugging and maintenance of the code will require much more time, which is more likely to waste days or weeks.
Jos (10584)
2013-2-11
0 个投票
Why not simply pass (part of) the structure around those functions?
类别
在 帮助中心 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!