How do you store multiple variables?

2 次查看(过去 30 天)
Hello,
I would like to ask how you store multiple variables and control them.
Let's say there are 26 variables.
a=123;
b=..;
c=...;
......
z=123;
variable a,b,c,d are related and e,f,g,h are related and so on.
Would you make vector A=[a,b,c,d], B=[e,f,g,h] and so on and bring them out whenever you need like A(1) or A(2)?
I am wondering if I make vector and store variables in it, how will other people know what that numbers are when they read my code? what is A(1) or A(2)?
Should I put comment for every single variables what they are next to the vector?
I woud like to know the rule of thumb when you store the variables.
THANK YOU!!
  6 个评论
dpb
dpb 2021-3-31
And, as Walter points out below, you can identify key subscripts with IDs through other variables (too bad MATLAB doesn't have enumerations for such purposes) to label things.
The key there in my mind is to:
  1. Don't go overboard in trying to name everything; just the key elements, and
  2. Use meaningful but still short names so code isn't cluttered by long names making even harder to read than just the integers.
Duckyoon Go
Duckyoon Go 2021-3-31
Thank you so much to everybody!! I got the idea!

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2021-3-30
Your code is not required to use numeric indices in the body of the code.
pitch = 1; yaw = 2;
...
cos(A(pitch))*sin(A(yaw))
  1 个评论
dpb
dpb 2021-3-31
I once had to maintain a production, real-time instrumentation code built around an indexing system of arrays. The original idea was one could make coding changes by changing the indexing variables. It never worked out that way in the end, but couldn't convince employer to rewrite.
It ended up with code that looked like--
FUNCTION ConvergeSelectedCentroid%
ConvergeROI% = Calib!(228)
DIM CalibTime AS TimeStruct, NormalTime AS TimeStruct
CalibTime.secs = SysCon!(132)
NormalTime.secs = SysCon!(131)
' Find the Carbon Escape centroid and then try to align to desired
AdjustedPeak% = FALSE
MaxIterations% = Calib!(219)
FOR i% = 1 TO MaxIterations%
Origin% = ConvergeROI% * 20
DeltaChannel! = Calib!(Origin%+20) * 100! - Calib!(Origin%+38)
IF ABS(DeltaChannel!)<Calib!(214) THEN
AdjustedPeak% = TRUE
EXIT FOR
END IF
Calib!(211) = Calib!(211) + DeltaChannel! * SysCon!(243) * SysCon!(244)
HVSet
...
And, more even more cryptic.
Calib!() and SysCon!() were global arrays.
It, of course, wasn't written in MATLAB... :(

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by