Creating a Dynamic Variable for Table Names

I am creating a code that will take multiple data sets and plot the data over top of eachother. To do this, I take csv files and make a table, (so I can compare individual variable.) I have a function that does this. However, the problem I have is the undicided amount of table I will need at a given time. This lead me into looking into dynamic variables. So at the top of my code I can input 5 for the number of tables (or any number of tables I need). Then each of my tables can be name A1, A2...A5. At the end of my code I need to be able to plot something like this:
plot(A1.red)
hold on
plot(A2.red)....
plot(A5.red)
Looking into dynamic variable this would not be the best way to go about. I could create a large amount of tables and use 'exist' to eliminatie ones I dont need but I would like it to be more fluid than that. Also I want this code to be able to used for a long time so allowing it to change depending on what the used needs would be ideal.

1 Comment

Stephen23
Stephen23 on 30 May 2023
Edited: Stephen23 on 30 May 2023
" However, the problem I have is the undicided amount of table I will need at a given time"
Why is that a problem? MATLAB arrays can be any size, and there is absolutely nothing stopping you from storing those tables in e.g. one cell array. All MATLAB users do this, what is stopping you?
"but I would like it to be more fluid than that."
Then you should be using one array and indexing. Just like MATLAB is designed for: the name MATALB comes from "MATrix LABoratory" and not from "Lets stick all of the data in thousands of separate variables and make accessing data slow and complex".
" I want this code to be able to used for a long time so allowing it to change depending on what the used needs would be ideal."
It sounds like a cell array would be ideal, or a non-scalar structure:

Sign in to comment.

Answers (2)

Can you dynamically create variables with numbered names like A1, A2, A3, etc.? Yes.
Should you do this? The general consensus is no. That Answers post explains why this is generally discouraged and offers several alternative approaches.
Depending on what you're trying to do a struct array each field of which contains a table, a cell array each cell of which contains a table, or a table with one variable containing an identifier are possibilities. In the latter case you would then use indexing (or perhaps groupsummary or other grouping functions) to operate on subsets of the table.
groups = "A" + randi(3, 10, 1);
data = randperm(10).';
T = table(groups, data)
T = 10×2 table
groups data ______ ____ "A3" 8 "A2" 5 "A2" 9 "A1" 7 "A3" 4 "A2" 1 "A3" 2 "A1" 10 "A2" 3 "A1" 6
group1 = T(T.groups == "A1", :)
group1 = 3×2 table
groups data ______ ____ "A1" 7 "A1" 10 "A1" 6

5 Comments

I think dynamic variable would still be better than this as I dont think this address my problem (Thank you for your responce though). I guess the real problem I have is: how do I give an unknown amount of tables a name? Like, I have the table so I am not creating it (or even worried about rearanging the data at this point), but I need something to call it. If I dont upfront call it something what would work better than. Ideal I would have been able to store it in a array or matric like each table be lebel under something like T(2,3) but I couldn't get that to work.
If I did want to try dynamic variable, where is the best place to start?
Store it in a cell array.
number_of_tables = randi([2 9])
number_of_tables = 3
for K = 1 : number_of_tables
SomeData = randi(9,3,1);
T{K} = table(SomeData);
end
T
T = 1×3 cell array
{3×1 table} {3×1 table} {3×1 table}
T{1}
ans = 3×1 table
SomeData ________ 2 8 6
T{2}
ans = 3×1 table
SomeData ________ 6 1 2
I guess the real problem I have is: how do I give an unknown amount of tables a name?
If you had ten thousand tables, would you really want to create ten thousand individual variables in the workspace? How about if you had a thousand tables and a thousand variables? Even a hundred would be unwieldy.
You can create dynamic variables, but I predict that your next question likely would be "How do I make my code faster?" to which the generally accepted answer is "Stop using dynamically created variables."
If the table with a group indicator doesn't meet your needs, perhaps a cell array would.
x = (1:10).';
T = cell(1, 5);
for k = 1:5
T{k} = table(x, x.^k, 'VariableNames', ["x", "x^"+k]);
end
T{3} % as an example
ans = 10×2 table
x x^3 __ ____ 1 1 2 8 3 27 4 64 5 125 6 216 7 343 8 512 9 729 10 1000
Stephen23
Stephen23 on 30 May 2023
Edited: Stephen23 on 30 May 2023
"I think dynamic variable would still be better..."
That is very unlikely. Your approach will be complex, slow, inefficient, and harder to debug: https://www.mathworks.com/matlabcentral/answers/304528-tutorial-why-variables-should-not-be-named-dynamically-eval
"... than this as I dont think this address my problem"
However it does address all of the other problems that you are about to cause with your bad data design.
It is very unlikely that your actual goal is to store lots of data with particular variable names. Most likely your goal is to process that data in a particular way... but instead of asking us a question like "how can I arrange my data so that I can efficiently and easily process my data like this..." you asked us about your own concept of how to do it (which is best avoided). Understand the difference: https://xyproblem.info/
The answer will be to use arrays. Because that is the simple, easy, and very efficient way to use MATLAB.
Thanks you Walter Roberson and Steven Lord. This exactly what I looking for. I didnt know this existed.
Thanks

Sign in to comment.

Since R2022b, you can plot variables from multiple tables and timetables in a single plot with stackedplot.
tbls = { array2table(rand(3,2)), array2table(rand(3,2)), array2table(rand(3,2)) }
tbls = 1×3 cell array
{3×2 table} {3×2 table} {3×2 table}
tbls{1}
ans = 3×2 table
Var1 Var2 _______ _______ 0.93152 0.32289 0.08929 0.23156 0.3017 0.54319
tbls{2}
ans = 3×2 table
Var1 Var2 _______ _______ 0.57526 0.38108 0.82018 0.04219 0.37221 0.79734
tbls{3}
ans = 3×2 table
Var1 Var2 _______ ________ 0.12006 0.23846 0.55645 0.91375 0.6885 0.084278
stackedplot(tbls, "Var1")
stackedplot(tbls, "Var1", CombineMatchingNames=false)

Products

Release

R2023a

Asked:

on 30 May 2023

Moved:

on 5 Jun 2023

Community Treasure Hunt

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

Start Hunting!