Creating a Dynamic Variable for Table Names
63 views (last 30 days)
Show older comments
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
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:
Answers (2)
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)
group1 = T(T.groups == "A1", :)
5 Comments
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.
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}
tbls{2}
tbls{3}
stackedplot(tbls, "Var1")
stackedplot(tbls, "Var1", CombineMatchingNames=false)
0 Comments
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!