How to copy data from one timetable to another

17 views (last 30 days)
Hi all,
I have three timetables (called Tmin, Tmax, Prec) with daily meteorological variables at two measurement sites (Site 1 and Site 2; I will come back to Site 3 in a moment). The data are from 1st July 2018 to now. Here an example of the Prec timetable:
353×3 timetable
time Site_1 Site_2 Site_3
____________________ ______ ______ ____
01-Jun-2018 00:00:00 0.2 0.2 NaN
02-Jun-2018 00:00:00 0 0 NaN
03-Jun-2018 00:00:00 0 0 NaN
...
I'm trying to add data from a public weather station (Site 3, currently all NaNs) for the same period. These data are available online in monthly csv files which I import in a loop into monthly timetables:
30×3 timetable
Date MinimumTemperature__C_ MaximumTemperature__C_ Rainfall_mm_
__________ ______________________ ______________________ ____________
2018-06-01 4.1 13.3 0
2018-06-02 3.5 13.6 0.2
2018-06-03 4.3 13.8 0
What I'm trying to do is to copy Site 3 data from each monthly timetable into a respective row in column 3 of my main timetables (Tmin, Tmax, Prec).
1) Is there a simple function to do that?
2) Is it a problem that the date format isn't the same? If so, how can I change it?
Many thanks in advance for help!

Accepted Answer

Adam Danz
Adam Danz on 17 May 2019
Edited: Adam Danz on 18 May 2019
Is there a simple function to do that?
Is it a problem that the date format isn't the same?
As long as they are in datetime format, it shouldn't be a problem.
There should be a way to specify that you want to merge the rainfall column of the second timetable (TT2) with the site_3 column of the first timetable (TT1) but after a brief search I haven't found that method (please leave comments if anyone finds it!). Alternatively you can:
  1. remove the unnecessary columns from TT2
  2. change the name of the rainfall column to "Site_3"
  3. remove the "Site_3" column from TT1
  4. Merge the two cleaned-up time tables.
That would look something like this:
% Remove columns in 2nd timetable that shouldn't be merged with table 1
TT2clean = removevars(TT2,{'MinimumTemperature__C_','MaximumTemperature__C_'});
% Rename Rainfall column to "Site_3"
TT2.Properties.VariableNames = strrep(TT2.Properties.VariableNames,'Rainfall_mm_','Site_3');
% Remove Site_3 from first timetable
TT1clean = removevars(TT1,'Site_3');
% Synchronize timetables
TT = synchronize(TT1clean,TT2clean);
  3 Comments
Peter Perkins
Peter Perkins on 4 Jun 2019
There should be a way to specify that you want to merge the rainfall column of the second timetable (TT2) with the site_3 column of the first timetable (TT1) but after a brief search I haven't found that method
Subscripting, but your removevars is equivalent:
TTmin= synchronize(TTmin(:,1:2),TTsite3(:,"MinimumTemperature__C_ "));
TTmin.Properties.VariableNames("MinimumTemperature__C_ ") = "Site_3";
It would be possible (in this case) to use join, which does let you specify left and right variables. But in joining by time assumes both inputs have all the times of interest.
Corymbiamaculata, it is possible to put all three measurements for all three sites in one timetable, if that is convenient for you: Convert TTmin, TTmax, and TTprec to tables, then put them in a timetable. Something like
TT = timetable(Tmin, Tmax,Tprec,'RowTimes',dt)

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!