add a vector as a timetable element

1 view (last 30 days)
Hi,
I would like to write the column of a matrix as element of a timetable (meaning a column for each time). in this timetable I have other variables that have a scalar value at each time. Any suggestion on how to do it?
Cheers,
Giacomo

Accepted Answer

Cris LaPierre
Cris LaPierre on 8 Dec 2020
Edited: Cris LaPierre on 8 Dec 2020
A column for each time? Do you mean row?
Just add it as a new variable in your timetable. Each column is typically a separate variable.
indoors = readtimetable('indoors.csv')
indoors = 60x2 timetable
Time Humidity AirQuality ___________________ ________ __________ 2015-11-15 00:00:24 36 80 2015-11-15 01:13:35 36 80 2015-11-15 02:26:47 37 79 2015-11-15 03:39:59 37 82 2015-11-15 04:53:11 36 80 2015-11-15 06:06:23 36 80 2015-11-15 07:19:35 36 80 2015-11-15 08:32:47 37 80 2015-11-15 09:45:59 37 79 2015-11-15 10:59:11 36 80 2015-11-15 12:12:23 37 80 2015-11-15 13:25:35 37 79 2015-11-15 14:38:46 36 83 2015-11-15 15:51:58 37 80 2015-11-15 17:05:10 36 80 2015-11-15 18:18:22 37 80
% Create temperatures
tempF = randi(100,[height(indoors),1]);
% Add vector of temperatures to timetable
indoors.Temp = tempF
indoors = 60x3 timetable
Time Humidity AirQuality Temp ___________________ ________ __________ ____ 2015-11-15 00:00:24 36 80 75 2015-11-15 01:13:35 36 80 17 2015-11-15 02:26:47 37 79 6 2015-11-15 03:39:59 37 82 92 2015-11-15 04:53:11 36 80 74 2015-11-15 06:06:23 36 80 98 2015-11-15 07:19:35 36 80 72 2015-11-15 08:32:47 37 80 65 2015-11-15 09:45:59 37 79 1 2015-11-15 10:59:11 36 80 84 2015-11-15 12:12:23 37 80 45 2015-11-15 13:25:35 37 79 23 2015-11-15 14:38:46 36 83 65 2015-11-15 15:51:58 37 80 5 2015-11-15 17:05:10 36 80 46 2015-11-15 18:18:22 37 80 10
  3 Comments
Cris LaPierre
Cris LaPierre on 9 Dec 2020
Still not a problem. Can we assume the times in your timtable rows align with the times of your matrix colums?
Updating the example slightly.
% Creating a 60x2 timetable
indoors = readtimetable('indoors.csv');
% Creating a matrix with 4 heights x 60 times (reduced for visual purposes only)
tempF = randi(100,[4,60]);
% Add temps to timetable under a variable Temp
% Transopose tempF so that rows are times, and columns are heights
indoors.Temp = tempF'
indoors = 60x3 timetable
Time Humidity AirQuality Temp ___________________ ________ __________ _______________________ 2015-11-15 00:00:24 36 80 4 16 47 59 2015-11-15 01:13:35 36 80 23 3 13 91 2015-11-15 02:26:47 37 79 76 34 90 13 2015-11-15 03:39:59 37 82 76 24 12 41 2015-11-15 04:53:11 36 80 92 76 25 39 2015-11-15 06:06:23 36 80 67 85 37 63 2015-11-15 07:19:35 36 80 20 4 29 68 2015-11-15 08:32:47 37 80 41 60 98 99 2015-11-15 09:45:59 37 79 64 48 15 14 2015-11-15 10:59:11 36 80 56 26 100 31 2015-11-15 12:12:23 37 80 67 74 55 74 2015-11-15 13:25:35 37 79 57 2 46 16 2015-11-15 14:38:46 36 83 84 54 16 35 2015-11-15 15:51:58 37 80 87 21 12 23 2015-11-15 17:05:10 36 80 61 73 34 78 2015-11-15 18:18:22 37 80 69 81 66 73
If you want each height to be its own variable, you can use the splitvars function.
indoors = splitvars(indoors,'Temp','NewVariableNames',["H1" "H2" "H3" "H4"])
indoors = 60x6 timetable
Time Humidity AirQuality H1 H2 H3 H4 ___________________ ________ __________ __ __ ___ __ 2015-11-15 00:00:24 36 80 4 16 47 59 2015-11-15 01:13:35 36 80 23 3 13 91 2015-11-15 02:26:47 37 79 76 34 90 13 2015-11-15 03:39:59 37 82 76 24 12 41 2015-11-15 04:53:11 36 80 92 76 25 39 2015-11-15 06:06:23 36 80 67 85 37 63 2015-11-15 07:19:35 36 80 20 4 29 68 2015-11-15 08:32:47 37 80 41 60 98 99 2015-11-15 09:45:59 37 79 64 48 15 14 2015-11-15 10:59:11 36 80 56 26 100 31 2015-11-15 12:12:23 37 80 67 74 55 74 2015-11-15 13:25:35 37 79 57 2 46 16 2015-11-15 14:38:46 36 83 84 54 16 35 2015-11-15 15:51:58 37 80 87 21 12 23 2015-11-15 17:05:10 36 80 61 73 34 78 2015-11-15 18:18:22 37 80 69 81 66 73
If you need help in accessing your data in the table, see this documentation page.
giacomo labbri
giacomo labbri on 9 Dec 2020
Thanks! This was very helpful!

Sign in to comment.

More Answers (0)

Categories

Find more on Tables in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!