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 :

 채택된 답변

Chetan Bhavsar
Chetan Bhavsar 2023년 7월 24일
편집: Chetan Bhavsar 2023년 7월 24일
% 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

sensor
sensor 2023년 7월 25일
Thanks @Chetan Bhavsar your answer sloved my issue.
sensor
sensor 2023년 7월 25일
@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
sensor
sensor 2023년 7월 26일
@Chetan Bhavsar you are right. Everything is okay. Thanks !

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

질문:

2023년 7월 24일

댓글:

2023년 7월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by