MATLAB Answers

How to split a table into sections of it dependent on the timestamp of another table

5 views (last 30 days)
Tomas Miguieles
Tomas Miguieles on 12 Jul 2020
Answered: Kavya Vuriti on 6 Aug 2020
Hi everyone. I'm running MatLab R2020a. I imported a .csv file into my live script using the "Column vectors" option. Doing that I added a 11814x1 datetime matrix (timestamp) and 112 different 11814x1 'double' matrices with recordings for each timestamp. The datetime table holds recordings from 2008 to 2010 by intervals of an hour and the format is 'dd/MM/yyyy h:mm'. For you to have an idea of what each of those recordings mean believe they are temperature recordings from 112 different locations along a beam from 01/2008 to 12/2010. Then, I compiled all those 'double' tables into a cell array using the following code:
VWfiles = [VW_LU1 VW_LU2 VW_LU3 VW_LU4 VW_LU5 VW_LU6 VW_LU7 VW_LU8 VW_LU9 VW_LU10 VW_LU11 VW_LU12 VW_LU13 VW_LU14 VW_LU15 VW_LU16 VW_LU17 VW_LU18 VW_LU19 VW_LU20 VW_LU21 VW_LU22 VW_LU23 VW_LU24 VW_LU25 VW_LU26 VW_LU27 VW_LU28 VW_LU29 VW_LU30 VW_LU31 VW_LU32 VW_LU33 VW_LU34 VW_LU35 VW_LU36 VW_LU37 VW_LU38 VW_LU39 VW_LU40 VW_LU41 VW_LU42 VW_LU43 VW_LU44 VW_LU45 VW_LU46 VW_LU47 VW_LU48 VW_LU49 VW_LU50 VW_LU51 VW_LU52 VW_LU53 VW_LU54 VW_LU55 VW_LU56 VW_LU57 VW_LU58 VW_LU59 VW_LU60 VW_LU61 VW_LU62 VW_LU63 VW_LU64 VW_LU65 VW_LU66 VW_LU67 VW_LU68 VW_LU69 VW_LU70 VW_LU71 VW_LU72 VW_LU73 VW_LU74 VW_LU75 VW_LU76 VW_LU77 VW_LU78 VW_LU79 VW_LU80 VW_LU81 VW_LU82 VW_LU83 VW_LU84 VW_LU85 VW_LU86 VW_LU87 VW_LU88 VW_LU89 VW_LU90 VW_LU91 VW_LU92 VW_LU93 VW_LU94 VW_LU95 VW_LU96 VW_LU97 VW_LU98 VW_LU99 VW_LU100 VW_LU101 VW_LU102 VW_LU103 VW_LU104 VW_LU105 VW_LU106 VW_LU107 VW_LU108 VW_LU109 VW_LU110 VW_LU111 VW_LU112];
columns = ["VW_LU1", "VW_LU2", "VW_LU3", "VW_LU4", "VW_LU5", "VW_LU6", "VW_LU7", "VW_LU8", "VW_LU9", "VW_LU10", "VW_LU11", "VW_LU12", "VW_LU13", "VW_LU14", "VW_LU15", "VW_LU16", "VW_LU17", "VW_LU18", "VW_LU19", "VW_LU20", "VW_LU21", "VW_LU22", "VW_LU23", "VW_LU24", "VW_LU25", "VW_LU26", "VW_LU27", "VW_LU28", "VW_LU29", "VW_LU30", "VW_LU31", "VW_LU32", "VW_LU33", "VW_LU34", "VW_LU35", "VW_LU36", "VW_LU37", "VW_LU38", "VW_LU39", "VW_LU40", "VW_LU41", "VW_LU42", "VW_LU43", "VW_LU44", "VW_LU45", "VW_LU46", "VW_LU47", "VW_LU48", "VW_LU49", "VW_LU50", "VW_LU51", "VW_LU52", "VW_LU53", "VW_LU54", "VW_LU55", "VW_LU56", "VW_LU57", "VW_LU58", "VW_LU59", "VW_LU60", "VW_LU61", "VW_LU62", "VW_LU63", "VW_LU64", "VW_LU65", "VW_LU66", "VW_LU67", "VW_LU68", "VW_LU69", "VW_LU70", "VW_LU71", "VW_LU72", "VW_LU73", "VW_LU74", "VW_LU75", "VW_LU76", "VW_LU77", "VW_LU78", "VW_LU79", "VW_LU80", "VW_LU81", "VW_LU82", "VW_LU83", "VW_LU84", "VW_LU85", "VW_LU86", "VW_LU87", "VW_LU88", "VW_LU89", "VW_LU90", "VW_LU91", "VW_LU92", "VW_LU93", "VW_LU94", "VW_LU95", "VW_LU96", "VW_LU97", "VW_LU98", "VW_LU99", "VW_LU100", "VW_LU101", "VW_LU102", "VW_LU103", "VW_LU104", "VW_LU105", "VW_LU106", "VW_LU107", "VW_LU108", "VW_LU109", "VW_LU110", "VW_LU111", "VW_LU112"];
% I also substracted each row from the first recording of each 'double' to get the change in temperature over time.
VWdelta = VWfiles - VWfiles(1,:);
Now, I have a the initial 11814x1 datetime matrix and a 11814x112 matrix with the temperature recordings.
I would like to split both matrices for the recordings of each year and group them into a new matrix (or any format that fits better). In other words, How can I split the datetime matrix into 3, one for each year (2008, 2009, 2010), and split the recordings matrix as well for each year of recordings?; Then group the recording data with the datetime data into one array for each year.
Extra: I would also like to plot the data from that newly created array. The idea is to have 1 figure with 4 plots, one for each random temperature column, and that each plot holds 'y' values for each year (so three lines). If you can add a code suggestion for that too, it would be awesome.

Answers (1)

Kavya Vuriti
Kavya Vuriti on 6 Aug 2020
From the question, I understand that you have two tables, one with timestamps in d-mmm-y HH:mm:ss format and the other with corresponding temperature recordings for 112 different locations. I am naming the timestamp table with a variable named timestamp. You can use year function to extarct years from the date-time matrix and use logical indexing to obtain indices corresponding to specific year. "Financial toolbox" is required to use year function. Here is the code snippet which does above tasks:
% Convert timestamp table to date-time format
timestamp_array = table2array(timestamp);
% Extract timestamps with year 2008 into a new timestamp table
timestamp_2008 = timestamp(year(timestamp_array) == 2008, :);
% Corresponding data
data_2008 = VWFiles(year(timestamp_array) == 2008, :);
% Grouping the data and timestamps
new_table_2008 = [timestamp_2008 data_2008];
Similarly, you can extract timestamps and data corresponding to 2009 and 2010 and group them.
Convert the table to array to plot the data. Sample code:
plot(table2array(new_table_2008(:, 1)), table2array(new_table_2008(:, 2:end)));
For having multiple plots in a single figure window, try using subplot.

Community Treasure Hunt

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

Start Hunting!