Adding additional data to previous table

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?

 Respuesta aceptada

dpb
dpb el 6 de Jul. de 2020
Editada: dpb el 6 de Jul. de 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 comentarios

Alexandra Philip
Alexandra Philip el 6 de Jul. de 2020
How do I append, should I use the save function?
What or how should the NewATcellfData be defined as?
dpb
dpb el 6 de Jul. de 2020
The ";" does the appending; the existing table above, the newly created one (unamed output of the cell2table() call) below contains the new record.
As said, the new data needs to be created the same way for each new record as was that for the original. In other words, it needs to look just like ATcellf above. It can have any number of records/rows; just all have to match up by column.
Alexandra Philip
Alexandra Philip el 7 de Jul. de 2020
Thank you. The code is running smoothly and is able to create a new row with the new data.
Alexandra Philip
Alexandra Philip el 7 de Jul. de 2020
Editada: dpb el 7 de Jul. de 2020
I am still having some trouble as it doesn't save the data from the previous run and I used:
ATcellf={AccelSN,Teststr,Accel0,Accel90,Accel180,Accel270, ...
Temp0,Temp90,Temp180,Temp270,VB,HB,VSF};
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'}
ATtablef=[ATtablef;cell2table(ATcellf,'VariableNames',ATtablef.Properties.VariableNames)]
Any suggestions?
dpb
dpb el 7 de Jul. de 2020
Entirely up to your code to save anything it needs. If you're running at separate times, then you'll have to have saved the previous data to file and re-read it to have the beginning position from last time--there's no magic way a variable is associated with data other than by creating it new or reading it from some previous saved source.
Why use the explicit name list in the above--that's a lot of typing and is available as my example shows from the Properties struct of the target table.
Of course, you have to have created the table or read in the previously saved one first...but that's a given.
Alexandra Philip
Alexandra Philip el 7 de Jul. de 2020
Editada: dpb el 7 de Jul. de 2020
How should I save the code then reread it? I was having trouble using the save function as I tried:
plotpath='filename';
save(plotpath, 'Tumble Data User Applicable.mat')
PLEASE! Use the "Code" button to format code lines...
You SAVE/LOAD the variable -- see the examples.
Your code above tries to save a variable named 'Tumble Data User Applicable.mat' to a file called 'filename' I strongly recommend against using file names with embedded blanks; they're a pain to deal with...
savepath='YourDesiredPathToSavedFiles'; % define the desired path somewhere
savefile='TumbleDataUserApplicable'; % and a file name
save(fullfile(savepath,savefile),'ATtablef') % save the desired table variable
When rerun, code needs to check if the table variable is available already or not, if not, go thru the reverse to load it.
U can look at uiget/putfile as one possible user interface to choose a file and location if this isn't going to be fixed but user-changeable.
Lots of examples of all of these things in the doc if you'll read them and dig more thoroughly...
Alexandra Philip
Alexandra Philip el 7 de Jul. de 2020
I was able to save the table. Still trying to figure out how to bring it back out and add the new data to the next row.
dpb
dpb el 7 de Jul. de 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 el 8 de Jul. de 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 el 8 de Jul. de 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...

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

el 6 de Jul. de 2020

Comentada:

dpb
el 8 de Jul. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by