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!