Connect correct temperature to another table based on time
3 views (last 30 days)
Show older comments
Dion Theunissen
on 4 Aug 2021
Answered: Peter Perkins
on 5 Aug 2021
I want to connect two tables with each other. I have one table A:
A = [13-aug-2020, 14-aug-2020, 15-aug-2020], [13.8, 12.2, 8.5];
B = [14-aug-2020, 14-aug-2020, 13-aug-2020], [155, 203, 200];
Now I want to combine those two tables with each other based on time like this:
B = [14-aug-2020, 14-aug-2020, 13-aug-2020], [155, 203, 200], [12.2, 12.2, 13.8];
Anyone who can help me with this?
I read the tables like this at the moment
clear all; close all; clc;
check = dir('*.xls');
list = [];
for i = 3
opts = spreadsheetImportOptions("NumVariables", 3);
% Specify sheet and range
opts.Sheet = "Sheet1";
opts.DataRange = "A2:C367";
% Specify column names and types
opts.VariableNames = ["Aug2020", "VarName2", "Temp"];
opts.VariableTypes = ["datetime", "double", "double"];
% Specify variable properties
opts = setvaropts(opts, "Aug2020", "InputFormat", "");
% Import the data
weatherYear = readtable("/Users/diontheunissen/Documents/Smart_Driver/RawData/weatherYear.xlsx", opts, "UseExcel", false);
Date = datetime(weatherYear.Aug2020);
weatherYear.Aug2020 = Date;
opts = spreadsheetImportOptions("NumVariables", 10);
% Specify sheet and range
opts.DataRange = "A2:J414";
% Specify column names and types
opts.VariableNames = ["UitgevoerdDoor", "Rapport", "ActiviteitType", "Lat", "Long", "Starttijd", "Duurms", "KilometerstandBegin", "GeredenAfstand", "TotaalVerbruik"];
opts.VariableTypes = ["categorical", "string", "string", "string", "string", "datetime", "double", "double", "double", "double"];
% Specify variable properties
opts = setvaropts(opts, "Rapport", "WhitespaceRule", "preserve");
opts = setvaropts(opts, ["UitgevoerdDoor", "Rapport", "ActiviteitType", "Lat", "Long"], "EmptyFieldRule", "auto");
opts = setvaropts(opts, "Starttijd", "InputFormat", "");
% Import the data
name = fullfile(check(i).folder, check(i).name);
Ritten = readtable(name, opts, "UseExcel", false);
Datum = datetime(Ritten.Starttijd);
Datum.Format = 'dd-MMM-yyy';
Ritten.Starttijd = Datum;
% pos=find(ismember(Datum,Date));
% for m = 1:length(Datum)
% for np = 1:length(Date)
% n = string(Datum(m));
% weatherYear.Temp(n)
% end
% end
end
0 Comments
Accepted Answer
Peter Perkins
on 5 Aug 2021
Use timetables. It's a one-liner:
>> A = timetable(datetime(["13-aug-2020";"14-aug-2020";"15-aug-2020"]), [13.8; 12.2; 8.5])
A =
3×1 timetable
Time Var1
___________ ____
13-Aug-2020 13.8
14-Aug-2020 12.2
15-Aug-2020 8.5
>> B = timetable(datetime(["14-aug-2020";"14-aug-2020";"13-aug-2020"]), [155; 203; 200])
B =
3×1 timetable
Time Var1
___________ ____
14-Aug-2020 155
14-Aug-2020 203
13-Aug-2020 200
>> join(B,A)
ans =
3×2 timetable
Time Var1_B Var1_A
___________ ______ ______
14-Aug-2020 155 12.2
14-Aug-2020 203 12.2
13-Aug-2020 200 13.8
0 Comments
More Answers (0)
See Also
Categories
Find more on Timetables 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!