MATLAB: Loop on Dynamic Variable

allocatedynamicfor loopiterationloopMATLABvariable

I am trying to create code with a loop to analyze data. I have several pieces of data named by variables where the only difference in name is the final number
i.e.
RBLab3_Dynamics_AngAccel_1
RBLab3_Dynamics_AngAccel_2
I am trying to figure out how I can write just one matlab code, and allocate (?) a letter or something of the like to represent the number, and then run the whole thing for a for loop 1:7 (I have seven trials). I also am trying to have parts of this code create variables ALSO defined by the number. In case I am not being clear, I mean I want to create a code where I can run a loop, and on the first iteration (1), I can both use an array called "RBLab3_Dynamics_AngAccel_1" and create an array called "Angle1"
Thank you for any help

Best Answer

When you are a beginner it seems like a cunning and fast way to store information, but actually it is really bad practice to name your variables dynamically. MATLAB is also not intended for this kind of variable naming: if you continue to include data in the variable names then you will find yourself fighting many more of these battles against MATLAB.
However when you use more appropriate storage for your data (and meta-data) then you will suddenly find lots of MATLAB functions that do many useful operations for you, quickly and easily.
In your case a much more robust solution would be to use structures , where you can include fields for each kind of data (e.g. Process type, Flow data, Temperature data, Notes, Units, etc), or cell arrays . There are many functions that support working on structures and cell arrays, and can access these data easily, and they can also be used in vectorized code (which is something you need to learn about). And yes, you can even define structure fieldnames dynamically .
Placing your data in a structure or cell array also makes it much easier to pass to functions: can you imagine the fight you would have trying to pass hundreds of dynamically named variables to a function?
If you have a newer version of matlab you can also use a table , which stores the data together in one array but also allows key-name access to the columns. This might be a good alternative for your data.
In case you are interested, here are some pages explaining why dynamically assigning variable names is a really bad idea in MATLAB: