Matching dates in two different tables and inserting value in separate column in second table when dates match.

I want to match a date from one table column, with a date from a second table, and when the dates match, insert a value in a separate column in the second table, corresponding to that date. For example the first table is a list of flowering dates each year and the second table is the daily weather data for those years i.e. the tables are different sizes. If the date in the first table matches the date in the weather data, I want to insert "flowering" in a separate column corresponding to that date in the second table. This is so so I can then do for example temperature summations leading up to flowering.

 Accepted Answer

That sounds very straightforward as long as you want an exact match for the date down to the second (I'm assuming your dates are stored as datetime
ismatch = ismember(yourfirsttable.datecolumn, yoursecondtable.datecolumn);
yoursecondtable.newcolumn = categorical(ismatch, [false true], {'not flowering', 'flowering'});
would be an option. Another option would be to label the new column flowering and just have true/false as values of the column:
yoursecondtable.flowering = ismatch;

3 Comments

Wendy Cameron comment mistakenly posted as an answer moved here:
Thank you very much, that works perfectly.
Would you mind if I asked another question.
If I wanted to match the dates from 3 different columns and get a categorical output in a single column of the second table column, can this be done? I tried this but of course it just overrides the previous entries.
Overall, I'm trying to come up with a column that shows budburst, flowering, veraison and harvest dates for grapevines in the same column for data covering about 20 years so I can then calculate for example, temperature summations between the events and thus look for trends. I can do this using separate columns and the above solution if necessary but thought it would be more streamlined if they were all in the one column.
Much appreciated. Wendy
I'm sure it can be done and probably fairly easily, however I don't completely understand what you want. An example with some data would make it much clearer.
Please find attached some sample data. Miniprac shows a column of budburst dates and a column of flowering dates. Mini dates is the spreadsheet I want to match the dates to and when the dates match write either "budburst" or "flowering" in the second column (which I have inserted as an example in Minidates). Using your method, I can do each column separately and then merge them but I thought it would be neater if it could be done in one step. My actual data of course is more detailed than this but this cut down version I hope indicates more simply what I am hoping to achieve.
Thank you. Wendy

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

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

Start Hunting!