Automatically define (global) variables

조회 수: 7 (최근 30일)
Ben
Ben 2016년 4월 29일
편집: Stephen23 2019년 6월 19일
I would like to define several global variables (P1,P2,P3,...,Pn), where n is a number to be defined. I my first attempt was the following one:
for i = 1:n
global strcat('P',num2str(i))
end
This does not work, what is the proper way to do this?
  댓글 수: 1
Stephen23
Stephen23 2016년 4월 29일
편집: Stephen23 2016년 4월 29일
"what is the proper way to do this?"
The proper way is: DON'T,
This is such a bad idea on so many levels:
  • globals... a major cause of beginners writing slow, buggy, impossible-to-read code. Globals are the least recommended way of passing data between workspaces.
  • dynamic variable names... a major cause of beginners writing slow, buggy, impossible-to-read code. MATLAB has a whole page advising to avoid string evaluation and creating variable names.
So lets combine the two worst code practices into one big mess of impossible-to-debug code, and win some obfuscated code prizes while we are at it.
Hanging a hurricane lamp out on the verandah is a great idea if you want to attract all of the bugs in the neighborhood. If you want to keep your verandah clean and tidy, do not use globals and dynamically defined variable names.
Search this forum to know more about these topics.

댓글을 달려면 로그인하십시오.

채택된 답변

Stephen23
Stephen23 2016년 4월 29일
편집: Stephen23 2019년 6월 19일

추가 답변 (3개)

Image Analyst
Image Analyst 2016년 4월 29일
In each function where you want to access those numbers, define a single global variable P
global P;
Then, later when you've discovered how many P you want, in any of the functions where you have declared P as global, do this
P = zeros(1,n);
Of course you could declare an array of cells, structures, integers, doubles, or whatever you need - just use the proper initialization function.
It's extremely bad practice to poof into existence an unknown-in-advance number of variables with different names. I mean, how would you refer to them? What if later you said newVar = P39, but there turned out to be no P39? It's much better handled with an array where you merely use an index, and the index can easily be compared to the length of the array to make sure you don't go past the end of the array.

Kevin
Kevin 2016년 4월 29일
If you really need to use global variable, then I would suggest using structure. Create global structure that stores all your global variables.
function foo
global S
S.x1 = [1 2 3];
S.A1 = [1 3 4; 3 6 8];
end

Ben
Ben 2016년 4월 30일
Thank you all for your input! Would you rather suggest to use cell arrays oder structures for ROI objects?
  댓글 수: 1
Image Analyst
Image Analyst 2016년 4월 30일
A structure array is simpler than a cell array and uses less overhead. Plus you don't have to worry about when to use parentheses, braces, or brackets. It's just a lot easier.
Like in a loop where you're calling imrect(), or whatever...
for k = 1 : 10
hRect = imrect(); % The function returns h, a handle to an imrect object.
P(k) = hRect;
end
Now P is a structure array where each element of P is a single structure, which is an ROI object.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by