Clear Filters
Clear Filters

How do you store multiple variables?

2 views (last 30 days)
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 Comments
dpb
dpb on 31 Mar 2021
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 on 31 Mar 2021
Thank you so much to everybody!! I got the idea!

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Mar 2021
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 Comment
dpb
dpb on 31 Mar 2021
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... :(

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!