passing lots of variables between function efficiently?
Afficher commentaires plus anciens
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?
Réponse acceptée
Plus de réponses (3)
Thorsten
le 11 Fév 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
le 11 Fév 2013
0 votes
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 commentaires
Kai
le 11 Fév 2013
Youssef Khmou
le 11 Fév 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
le 11 Fév 2013
Going to disk is always going to be slower than staying in memory.
Youssef Khmou
le 11 Fév 2013
Modifié(e) : Youssef Khmou
le 11 Fév 2013
ok then put all your functions in one signle M-file and make your variables global .
>>doc global
Jan
le 11 Fév 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)
le 11 Fév 2013
0 votes
Why not simply pass (part of) the structure around those functions?
Catégories
En savoir plus sur Whos dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!