Clear Filters
Clear Filters

Creating a Dynamic Variable for Table Names

59 views (last 30 days)
Zach
Zach on 30 May 2023
Moved: Walter Roberson on 5 Jun 2023
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)

Steven Lord
Steven Lord on 30 May 2023
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
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.
Zach
Zach on 30 May 2023
Moved: Walter Roberson on 5 Jun 2023
Thanks you Walter Roberson and Steven Lord. This exactly what I looking for. I didnt know this existed.
Thanks

Sign in to comment.


Seth Furman
Seth Furman on 5 Jun 2023
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)

Categories

Find more on Data Type Identification in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!