MATLAB Answers

Adding additional data to previous table

7 views (last 30 days)
Alexandra Philip
Alexandra Philip on 6 Jul 2020
Commented: dpb on 8 Jul 2020
I am attempting to find a way to add in additional data to the next row of this table after every run still retaining the previous data( there isn't a certain number of runs, just whenever the user wants to run it).
ATtablef=cell2table(ATcellf);
ATtablef.Properties.VariableNames={'Accel S/N' 'Test date' 'Accel_0' 'Accel_90' 'Accel_180' 'Accel_270'...
'Temp_0' 'Temp_90' 'Temp_180' 'Temp_270','Vertical Bias','Horizontal Bias','Vertical Scalefactor'}
Any suggestions to acheive this?

  0 Comments

Sign in to comment.

Accepted Answer

dpb
dpb on 6 Jul 2020
Edited: dpb on 6 Jul 2020
If it's not time-critical, it's trivial to just dynamically append -- presuming the table exists initially and the content of the cell array is one or more rows with the proper number and types of all the variables in the table (every row has to be complete w/ no missing or extra values and of the same type)
ATtablef=[ATtablef;cell2table(NewATcellfData)];
ILLUSTRATION: Given the apparent confusion expressed in follow-up comment.
A table happened to have in workspace from another Q? from another poster looked at is, in part:
>> ttmp
ttmp =
8×10 table
DateTimeUTC AIMHeading AccX AccY AccZ Surge Sway Heave Roll Pitch
____________________ __________ _______ _______ _______ _______ _______ _______ _______ _______
09-Aug-2019 07:01:01 228.9 0.3864 0.3124 0.0544 0.2421 0.3638 0.0059 -1.5691 -0.3287
09-Aug-2019 07:01:02 228.9 -0.1636 -0.03 -0.0195 -0.1808 0.1131 -0.0466 -1.9137 0.0113
09-Aug-2019 07:01:03 229.1 -0.2974 -0.2663 -0.0237 -0.3529 -0.0107 -0.0645 -1.299 -1.0392
09-Aug-2019 07:01:04 229.3 -0.1462 -0.1184 0.04 -0.2197 0.1167 -0.0527 -0.6783 -2.0269
09-Aug-2019 07:01:05 229.4 0.0183 0.2937 0.0223 -0.029 0.2389 -0.0537 -1.0576 -2.0764
09-Aug-2019 07:01:06 229.4 0.3044 0.0866 -0.0578 -0.0044 0.0772 -0.0475 -1.7729 -1.1997
09-Aug-2019 07:01:07 229.4 0.0308 -0.1888 -0.0367 -0.2413 -0.1377 0.008 -1.6663 -0.7888
09-Aug-2019 07:01:08 229.3 -0.3493 -0.1879 0.0397 -0.4157 -0.1401 0.076 -0.9687 -1.3592
>>
% let's make and append a new row...use current time and average of existing other data...
>> newRec=[{datetime(now,'ConvertFrom','datenum')} (num2cell(mean(ttmp{:,2:end})))]
newRec =
1×10 cell array
{[06-Jul-2020 15:56:45]} {[229.2125]} {[-0.0271]} {[-0.0123]} {[0.0023]} {[-0.1502]} {[0.0777]} {[-0.0219]} {[-1.3657]} {[-1.1009]}
>> cell2table(newRec) % see what converting to a table will yield...
ans =
1×10 table
newRec1 newRec2 newRec3 newRec4 newRec5 newRec6 newRec7 newRec8 newRec9 newRec10
____________________ _______ _________ _________ _________ ________ _______ _________ _______ ________
06-Jul-2020 15:56:45 229.21 -0.027075 -0.012337 0.0023375 -0.15021 0.07765 -0.021887 -1.3657 -1.1009
>>
% looks like what expected for values, so let's append to the existing table...
>> ttmp=[ttmp;cell2table(newRec,'VariableNames',ttmp.Properties.VariableNames)]
ttmp =
9×10 table
DateTimeUTC AIMHeading AccX AccY AccZ Surge Sway Heave Roll Pitch
____________________ __________ _________ _________ _________ ________ _______ _________ _______ _______
09-Aug-2019 07:01:01 228.9 0.3864 0.3124 0.0544 0.2421 0.3638 0.0059 -1.5691 -0.3287
09-Aug-2019 07:01:02 228.9 -0.1636 -0.03 -0.0195 -0.1808 0.1131 -0.0466 -1.9137 0.0113
09-Aug-2019 07:01:03 229.1 -0.2974 -0.2663 -0.0237 -0.3529 -0.0107 -0.0645 -1.299 -1.0392
09-Aug-2019 07:01:04 229.3 -0.1462 -0.1184 0.04 -0.2197 0.1167 -0.0527 -0.6783 -2.0269
09-Aug-2019 07:01:05 229.4 0.0183 0.2937 0.0223 -0.029 0.2389 -0.0537 -1.0576 -2.0764
09-Aug-2019 07:01:06 229.4 0.3044 0.0866 -0.0578 -0.0044 0.0772 -0.0475 -1.7729 -1.1997
09-Aug-2019 07:01:07 229.4 0.0308 -0.1888 -0.0367 -0.2413 -0.1377 0.008 -1.6663 -0.7888
09-Aug-2019 07:01:08 229.3 -0.3493 -0.1879 0.0397 -0.4157 -0.1401 0.076 -0.9687 -1.3592
06-Jul-2020 15:56:45 229.21 -0.027075 -0.012337 0.0023375 -0.15021 0.07765 -0.021887 -1.3657 -1.1009
>>
and Voila! A new record has been added.
NB: The only "trick" not mentioned before is that have to use the names to match up if don't create variables of those names and append them variable-by-variable in the table() statement...
How you choose to build the new record(s) is entirely up to you, but that's all there is to appending to existing table.

  11 Comments

Show 8 older comments
dpb
dpb on 7 Jul 2020
  1. Create an initial table of desired structure.
  2. SAVE this table to .mat file
  3. LOAD the same file you previously saved.
  4. add a new row to the table variable in memory following example code above for pattern
  5. (re)SAVE the updated table
Rinse and repeat above steps 3. thru 5. as desired.
Your code should probably have two sections or have two separate functions--first to create the table and save it initially (or create a new table for new test or whatever reason), and secondly to update/populate the table with new data.
The first section could, of course, also add whatever data are presently available at the time at creation but it could just consist of the header section initially.
Alexandra Philip
Alexandra Philip on 8 Jul 2020
I was able to retain the prior data and add on the new data, Thank you! How should I repeat to still retain the data and add on new data for new runs? Should I use a loop?
dpb
dpb on 8 Jul 2020
That's up to how you want to use this...and how/when new data are available. We don't know about those details...

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!