find common timestamp of two different matrix

I have two kinds of data heat production (HP) and temperature. Both data have timestamp values in the first column and second column include HP and temperature values. The size of both data is unequal. Firstly I want to match the timestamp of HP with the timestamp of temperature and than create a third column that include the HP values with the matched timestamp of the temperature, while making unmatched values zeros. See the attached images and data as mat file.
data 1:
data 2:
Output I want :

 Accepted Answer

% Load Mat file in Base Workspace
load("data.mat")
% Convert the timestamps to strings, including only up to the minute
Temperature.Timestamp_temp = cellstr(datetime(Temperature.Timestamp_temp,'Format','yyyy-MM-dd HH:mm'));
HP.Timestamp_HP = cellstr(datetime(HP.Timestamp_HP,'Format','yyyy-MM-dd HH:mm'));
% Initialize the new table from the Temperature table
new_table = Temperature;
% Add a new column for HP, initializing with zeros
new_table.HP = zeros(height(new_table), 1);
% Fill in the HP data where the times match
[~, idx1, idx2] = intersect(new_table.Timestamp_temp, HP.Timestamp_HP);
new_table.HP(idx1) = HP.Var2(idx2);
disp(new_table(1:50,:));
Timestamp_temp Var2 HP ____________________ ______ _____ {'2022-08-23 11:53'} 32.473 0 {'2022-08-23 11:53'} 32.473 0 {'2022-08-23 11:54'} 32.508 0 {'2022-08-23 11:54'} 32.527 0 {'2022-08-23 11:55'} 32.545 0 {'2022-08-23 11:55'} 32.598 0 {'2022-08-23 11:56'} 32.653 0 {'2022-08-23 11:56'} 32.724 0 {'2022-08-23 11:57'} 32.795 0 {'2022-08-23 11:57'} 32.867 0 {'2022-08-23 11:58'} 32.921 0 {'2022-08-23 11:58'} 32.975 0 {'2022-08-23 11:59'} 33.029 0 {'2022-08-23 11:59'} 33.064 0 {'2022-08-23 12:00'} 33.118 0 {'2022-08-23 12:00'} 33.172 0 {'2022-08-23 12:01'} 33.209 0 {'2022-08-23 12:01'} 33.279 0 {'2022-08-23 12:02'} 33.333 0 {'2022-08-23 12:02'} 33.405 0 {'2022-08-23 12:03'} 33.476 394.6 {'2022-08-23 12:03'} 33.549 0 {'2022-08-23 12:04'} 33.566 0 {'2022-08-23 12:04'} 33.602 0 {'2022-08-23 12:05'} 33.62 0 {'2022-08-23 12:05'} 33.622 0 {'2022-08-23 12:06'} 33.62 0 {'2022-08-23 12:06'} 33.584 0 {'2022-08-23 12:07'} 33.548 0 {'2022-08-23 12:07'} 33.531 0 {'2022-08-23 12:08'} 33.495 0 {'2022-08-23 12:08'} 33.459 0 {'2022-08-23 12:09'} 33.405 385.5 {'2022-08-23 12:09'} 33.352 0 {'2022-08-23 12:10'} 33.297 0 {'2022-08-23 12:10'} 33.244 0 {'2022-08-23 12:11'} 33.172 0 {'2022-08-23 12:11'} 33.118 0 {'2022-08-23 12:12'} 33.046 374 {'2022-08-23 12:12'} 33.011 0 {'2022-08-23 12:13'} 32.957 0 {'2022-08-23 12:13'} 32.903 0 {'2022-08-23 12:14'} 32.867 0 {'2022-08-23 12:14'} 32.832 0 {'2022-08-23 12:15'} 32.795 0 {'2022-08-23 12:15'} 32.795 0 {'2022-08-23 12:16'} 32.795 0 {'2022-08-23 12:16'} 32.814 0 {'2022-08-23 12:17'} 32.832 0 {'2022-08-23 12:17'} 32.851 0
Please let me know if it is working as per expected or not.
also let me know if any part you didnt understood.

4 Comments

Thanks @Chetan Bhavsar your answer sloved my issue.
@Chetan Bhavsar I check again and found there are some non-existent values of HP in new_table. However the existent values are correctly matched with the timestamps. Is it possible to delete the unwanted/on-existent values form the HP (third column)? see the attached image.
@Mohan kand It does Exist Please check below!
% Load Mat file in Base Workspace
load("data.mat")
disp(HP(16150:16155,:));
Timestamp_HP Var2 ________________ _____ 19-12-2022 08:54 972.3 23-08-2022 12:03 394.6 23-08-2022 12:12 374 23-08-2022 12:21 406.4 23-08-2022 12:32 436.2 23-08-2022 12:41 447.8
@Chetan Bhavsar you are right. Everything is okay. Thanks !

Sign in to comment.

More Answers (0)

Products

Asked:

on 24 Jul 2023

Commented:

on 26 Jul 2023

Community Treasure Hunt

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

Start Hunting!