passing lots of variables between function efficiently?
Ältere Kommentare anzeigen
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?
Akzeptierte Antwort
Weitere Antworten (3)
Thorsten
am 11 Feb. 2013
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
am 11 Feb. 2013
0 Stimmen
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 Kommentare
Kai
am 11 Feb. 2013
Youssef Khmou
am 11 Feb. 2013
i do not think so, anyway i proposed a way out, use tic toc commands with load and without load and conclude .
Walter Roberson
am 11 Feb. 2013
Going to disk is always going to be slower than staying in memory.
Youssef Khmou
am 11 Feb. 2013
Bearbeitet: Youssef Khmou
am 11 Feb. 2013
ok then put all your functions in one signle M-file and make your variables global .
>>doc global
Jan
am 11 Feb. 2013
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)
am 11 Feb. 2013
0 Stimmen
Why not simply pass (part of) the structure around those functions?
Kategorien
Mehr zu Whos finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!