- What's in the tables? Are all of the table variables the same class? Are they all strings, numeric, or logical, for example? Or does the table contain heterogeneous data? Knowing this will be helpful to suggest an optimal solution.
- When you say "store the data" does that mean you are saving the data to and loading it from a mat file?
- How tall are the tables typically? I can imagine that master table might grow fairly large, quickly.
Append tables with a new variable
16 views (last 30 days)
Show older comments
Marcus Glover
on 23 Feb 2024
Edited: Marcus Glover
on 23 Feb 2024
Hello. Perhaps this is more of a general programming style question, so if I should reevaluate my methods please feel free to suggest a better way.
I collect and process data from several different machines and am saving the data as tables in .mat files.- I run my processing script automatically every day, though not every machine sends new data every day. That script gives me a result table new_data, which contans heterogeneous data consisting of categorical, numeric ans string variables.
I have one master table with all data that I load and append with a simple square bracket [new_data;all_data] notation, and I also have tables for each individual machine that I similarly append based on if there is new data for that machine. I do this perhaps out of laziness so I can just double click and load one machine's data simply rather than loading and sorting the master all_data table. I also figure this may save time as I will not load or create tables that do not have new data.
My question is what is the best course of action when I think of some new measure I want to make that would result in adding a new variable to all the existing tables? It won't happen too often, but probably will. As of now the main table is ~3000 rows but growing by about 100 rows per week- I don't yet have processing time or space concerns but would like to follow best practices.
Also a possibility is that I no longer want a variable and would like to drop it.
Would I manually add the new varable with dummy/blank data to all the tables every time I plan on adding a variable?
I thought of checking to see if the variables all match in a loop with strcmp every time I append, but that seems like it may be a bit much as I have many variables and will probably only add variables once or twice a year or so.
2 Comments
Adam Danz
on 23 Feb 2024
Great questions, Marcus. I've got some clarifying questions.
Accepted Answer
Walter Roberson
on 23 Feb 2024
T1 = table([1:5].')
T2 = table([6:8].', [3;1;4])
vn1 = T1.Properties.VariableNames;
vn2 = T2.Properties.VariableNames;
new_vars = setdiff(vn2, vn1)
T = T1;
for K = 1 : length(new_vars)
newvar = new_vars{K};
T.(newvar) = nan(height(T), 1);
end
T = [T; T2]
1 Comment
More Answers (0)
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!