# Extracting data from timetable

2 views (last 30 days)
Thomas Lees on 28 Jun 2022
Answered: Star Strider on 28 Jun 2022
I have a timetable with multiple variables which vary spatially and temporally and spatially across a transect.
The variables are recorded at millimeter and minutely intervals. These are shown below as 'V_1, V_2, V_3, V_4, V_5... V_300'. The number here refers to the millimeter at which the measurements were taken, at time steps of t1, t2, t3... tn.
One variable ('Event') occurs at discrete time points and at discrete locations along the transect. Event data is either not present (NaN), or present (recorded as a number). For the 'present' events, the number refers to the spatial dimension: the millimeter along the transect at which tte event occurred.
The picture below shows a simplified view of the data, with annotations showing what I'd like to do.
I would like to:
1. Output the value of the environmental variable at the time of each 'Event'.
2. Calculate and output the change in each environmental variable for the preceding n timesteps that lead up to each 'Event'.
Is this possible, and how would I got about it?
Thank you very much.

Star Strider on 28 Jun 2022
I am not certain what you want.
This should get you started —
tv = minutes(0:15).'; % Create Data, Table & Timetable
V = sort(rand(numel(tv), 4)*100);
Event = NaN(size(tv));
Event(6:5:end) = 1:3;
T1 = table(tv,V(:,1),V(:,2),V(:,3),V(:,4),Event, 'VariableNames',{'Time(min)','V1','V2','V3','V4','Event'});
TT1 = table2timetable(T1)
TT1 = 16×5 timetable
Time(min) V1 V2 V3 V4 Event _________ ______ ______ ______ ______ _____ 0 min 7.8016 4.3482 2.6771 1.868 NaN 1 min 11.65 6.2408 2.9078 11.692 NaN 2 min 27.461 8.4738 8.1575 17.296 NaN 3 min 29.376 11.939 27.023 17.797 NaN 4 min 32.054 21.408 29.515 22.26 NaN 5 min 33.782 40.783 35.796 27.53 1 6 min 34.252 45.865 40.116 32.314 NaN 7 min 42.098 47.42 42.998 35.885 NaN 8 min 64.39 52.906 65.534 42.117 NaN 9 min 66.281 55.008 68.684 48.175 NaN 10 min 69.237 59.847 87.426 50.402 2 11 min 70.956 60.483 90.498 52.169 NaN 12 min 73.418 66.35 91.692 84.902 NaN 13 min 75.665 66.668 95.33 87.244 NaN 14 min 79.617 81.282 95.451 90.413 NaN 15 min 89.86 90.803 98.999 93.516 3
EvIdx = find(~ismissing(TT1.Event)); % Event Index Vector
TTs = TT1(EvIdx(1):EvIdx(2)-1,1:end-1) % First Section
TTs = 5×4 timetable
Time(min) V1 V2 V3 V4 _________ ______ ______ ______ ______ 5 min 33.782 40.783 35.796 27.53 6 min 34.252 45.865 40.116 32.314 7 min 42.098 47.42 42.998 35.885 8 min 64.39 52.906 65.534 42.117 9 min 66.281 55.008 68.684 48.175
td(1) = TTs.('Time(min)')(end) - TTs.('Time(min)')(1);
Vd(1,:) = TTs{end,:} - TTs{1,:};
for k = 1:numel(EvIdx)-1
TTs = TT1(EvIdx(k):EvIdx(k+1)-1,1:end-1) % Intermediate Sections
td(k+1) = TTs.('Time(min)')(end) - TTs.('Time(min)')(1);
Vd(k+1,:) = TTs{end,:} - TTs{1,:};
end
TTs = 5×4 timetable
Time(min) V1 V2 V3 V4 _________ ______ ______ ______ ______ 5 min 33.782 40.783 35.796 27.53 6 min 34.252 45.865 40.116 32.314 7 min 42.098 47.42 42.998 35.885 8 min 64.39 52.906 65.534 42.117 9 min 66.281 55.008 68.684 48.175
TTs = 5×4 timetable
Time(min) V1 V2 V3 V4 _________ ______ ______ ______ ______ 10 min 69.237 59.847 87.426 50.402 11 min 70.956 60.483 90.498 52.169 12 min 73.418 66.35 91.692 84.902 13 min 75.665 66.668 95.33 87.244 14 min 79.617 81.282 95.451 90.413
Out = array2table([minutes(td(:)), Vd], 'VariableNames',{'TimeSpan(min)',TT1.Properties.VariableNames{1:4}})
Out = 3×5 table
TimeSpan(min) V1 V2 V3 V4 _____________ ______ ______ ______ ______ 4 32.498 14.225 32.888 20.645 4 32.498 14.225 32.888 20.645 4 10.38 21.434 8.0243 40.011
It takes the differences between the beginning and end values of each secton and saves them to ‘td’ and ‘Vd’ then puts those results in a table.
.

R2022a

### Community Treasure Hunt

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

Start Hunting!