# General variable creation question

1 view (last 30 days)
Ines Shekhovtsov on 22 Mar 2023
Commented: Stephen23 on 24 Mar 2023
Hi,
I am still new to programming in Matlab and i tend to code in a way that generates a lot of variables in a workspace. Generally it's fine but i would like to know what my other options are to minimize workspace clutter. For an example, i am extracitng a region of interest (ROI) from some EMG data using the same start and end index, so my code snippet looks like this:
RSOL_ROI = RSOL(start_indexEMG:end_indexEMG);
RMG_ROI = RMG(start_indexEMG:end_indexEMG);
RTA_ROI = RTA(start_indexEMG:end_indexEMG);
RMH_ROI = RMH(start_indexEMG:end_indexEMG);
RVL_ROI = RVL(start_indexEMG:end_indexEMG);
RRF_ROI = RRF(start_indexEMG:end_indexEMG);
I plan to plot the _ROI variables later so i still want them stored somewhere where i can call them later. Can someone suggest a way to write code that generates the 6 _ROI variables but doesn't store them in workspace? And just for learining purposes can someone write a for loop for this as well? I have tried a few times and did not seem to work.
EDIT: I know i can place the variables into a struct. As a quick question within this question what if i have a struct with the following 6 variables stored: RSOL,RMG,RTA,RMH,RVL,and RRF. And the struct is named A. Is there a way to call the variables by direct name in my script. Basically something like X=2*RSOL. instead of having to say X=2*A.RSOL. Hope that makes sense!

Joe Vinciguerra on 22 Mar 2023
1. Instead of using start_indexEMG and end_indexEMG, you could say index = [yourStartIndex : yourEndIndex]. Then use RSOL_ROI = RSOL(index) for example.
2. use clear to programatically delete variables when you're done with them to keep your workspace clean.
3. You "still want them stored somewhere". Well that "somewhere" is the workspace. You could, if it's appropriate to your application, save varaibles to a .MAT file, then load that file into your workspace when it's needed.
4. It is not recommended to generate individual variables in a loop.
5. To call a field in a structure use the structure variable name, such as A.RSOL. You can't just call RSOL and expect to get the field within the A structure.
6. To generate a structure in a loop would be something like this:
names = {'RSOL_ROI'
'RMG_ROI'
'RTA_ROI'
'RMH_ROI'
'RVL_ROI'
'RRF_ROI'}
A = struct()
for i = 1:length(names)
A.(names{i}) = [];
end
Ines Shekhovtsov on 23 Mar 2023
Thank you!

John D'Errico on 22 Mar 2023
Don't generate a zillion variables. Instead, learn to use arrays. Cell arrays, structs, multi-dimensional arrays. This will leave you with ONE variable to chase around.
It is just a habit you need to learn as good programming practice.
Stephen23 on 24 Mar 2023
One important step is to understand that meta-data (like the categories RSOL, RMG, RTA, RMH, RVL, and RRF) are data. And data belong inside variables, not in the code itself. Once you do that, then using arrays to store and process your data will be easier... and your code will be simpler and more generalizable as well.

### Categories

Find more on Matrix Indexing in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!