Matching datenum from multiple arrays

3 visualizaciones (últimos 30 días)
hanif hamden
hanif hamden el 10 de Jun. de 2023
Respondida: Simon Chan el 10 de Jun. de 2023
I have 2 files and need to match these two data by matching it with the same datenum. However, i still get an error like this
Error using horzcat
Dimensions of arrays being concatenated are not consistent.
Error in VSEPdatabase (line 100)
Out = [NBathy NGNSS];
It seems that the line of NBathy and line of NGNSS is not the same although i have executed it using ismember. Can anyone help on this matter?
Here is my coding and the attached 2 files.
clc; clear all; close all;
%% INPUT FILE
% Input Filename
GNSSfile = 'Smoothing_DAY1_120s.txt';
BATHYfile= 'AB KH UKUR1_D1.txt';
%Input File GNSS
fid1=fopen(GNSSfile); % _TPS
A = textscan(fid1,'%f %s %s %f %f %f %f ','HeaderLines',1); % read file;
Npt = A{1}; date1 =string(A{2}); time1 =string(A{3});
Yi1 = A{4}; Xi1 = A{5}; Ellh = A{6}; EllhF = A{7};
str1 = date1 + ' ' + time1;
dt1 = datetime(str1,'InputFormat','yyyy/MM/dd HH:mm:ss');
dtnum1 = datenum(dt1);
arr_gnss = [dtnum1 Yi1 Xi1 EllhF];
% Input File Bathy
fid2=fopen(BATHYfile); % _TPS
B = textscan(fid2,'%s %s %s %s %f %f %f '); % read file;
date2 =string(B{3}); time2 =string(B{4});
Yi2 = B{5}; Xi2 = B{6}; depth = B{7};
str2 = date2 + ' ' + time2;
dt2 = datetime(str2,'InputFormat','yyyyMMdd HH:mm:ss');
dtnum2 = datenum(dt2);
arr_bathy = [dtnum2 Yi2 Xi2 depth];
%% SYNCHRONIZE THE TIME OBSERVATION (BATHY & GNSS)
r1 = ismember(arr_gnss(:,1),arr_bathy(:,1));
r2 = ismember(arr_bathy(:,1),arr_gnss(:,1));
NGNSS = arr_gnss(r1,:);
NBathy = arr_bathy(r2,:);
Out = [NBathy NGNSS];

Respuestas (2)

Saurabh
Saurabh el 10 de Jun. de 2023
Hye @hanif hamden, I hope You are doing well.
The error message suggests that the dimensions of the two arrays being concatenated in line 100 (`Out = [NBathy NGNSS];`) do not match. This can happen if there are some rows in either `NBathy` or `NGNSS` that do not have a matching row in the other array.
To fix this error, you can modify the `ismember` function calls to include the `'rows'` option, which will search for matches based on all columns in the input arrays instead of just the first column. Here's the updated code:
% Synchronize the time observation (BATHY & GNSS)
[~,r1] = ismember(arr_gnss(:,1:3),arr_bathy(:,1:3),'rows');
[~,r2] = ismember(arr_bathy(:,1:3),arr_gnss(:,1:3),'rows');
NGNSS = arr_gnss(r1,:);
NBathy = arr_bathy(r2,:);
In this updated code, we include the `'rows'` option in the `ismember` calls to search for rows with matching values in all columns (`arr_gnss(:,1:3)` denotes the first three columns of `arr_gnss` which are date, time, and coordinates; `arr_bathy(:,1:3)` denotes the first three columns of `arr_bathy` which are date, time, and coordinates). We then use the resulting indices to extract the matching rows from `arr_gnss` and `arr_bathy` and assign them to `NGNSS` and `NBathy`, respectively.
This code should ensure that only rows with matching date, time, and coordinates are included in both `NGNSS` and `NBathy`. Note that if either `NGNSS` or `NBathy` is empty, it means that there are no matching rows in the other array, and concatenating the two arrays will still result in a dimension mismatch error. In that case, you should check your input files and make sure that the data is correct and in the expected format.

Simon Chan
Simon Chan el 10 de Jun. de 2023
If your final goal is to extract the information from these 2 files with the same date and time, use function innerjoin.
For the second txt file, you may add Variable Name to each column since there is no header in this file.
% Read the first file
data1 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1407529/Smoothing_DAY1_120s.txt','VariableNamingRule','preserve');
DateTime1 = datetime(data1.Date,'InputFormat','yyyy/MM/dd') + data1.Time;
data1.Date = DateTime1; % Update the datetime for each row and put in the 2nd column
data1.Time=[]; % Remove the unnecessary column
% Read the second file
data2 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1407534/AB%20KH%20UKUR1_D1.txt');
DateTime2 = datetime(string(data2.Var3),'InputFormat','yyyyMMdd')+data2.Var4;
data2.Var3 = DateTime2; % Update the datetime for each row and put in the 3rd column
data2.Var4=[]; % Remove the unnecessary column
data2.Properties.VariableNames{3}='Date'; % Give the Variable Name to the the datetime column
T = innerjoin(data1,data2) % Use function innerjoin
T = 2684×11 table
Pt Date Latitude Longitude Ellh Ellh(f120s) Var1 Var2 Var5 Var6 Var7 __ ____________________ ________ _________ ______ ___________ _________ __________ __________ __________ ______ 2 05-Dec-2022 03:15:01 1.3721 103.64 13.843 13.832 {'EVENT'} {'T.1108'} 6.2743e+05 1.5155e+05 -15.33 4 05-Dec-2022 03:15:03 1.3721 103.64 13.923 13.829 {'EVENT'} {'T.1109'} 6.2744e+05 1.5155e+05 -15.53 6 05-Dec-2022 03:15:05 1.3721 103.64 13.806 13.828 {'EVENT'} {'T.1110'} 6.2745e+05 1.5154e+05 -15.67 8 05-Dec-2022 03:15:07 1.3721 103.64 13.837 13.825 {'EVENT'} {'T.1111'} 6.2745e+05 1.5154e+05 -15.75 10 05-Dec-2022 03:15:09 1.3722 103.64 13.848 13.823 {'EVENT'} {'T.1112'} 6.2746e+05 1.5154e+05 -15.86 12 05-Dec-2022 03:15:11 1.3722 103.64 13.82 13.82 {'EVENT'} {'T.1113'} 6.2746e+05 1.5154e+05 -16.09 14 05-Dec-2022 03:15:13 1.3722 103.64 13.863 13.818 {'EVENT'} {'T.1114'} 6.2747e+05 1.5154e+05 -16.07 16 05-Dec-2022 03:15:15 1.3722 103.64 13.892 13.815 {'EVENT'} {'T.1115'} 6.2747e+05 1.5153e+05 -16.29 18 05-Dec-2022 03:15:17 1.3723 103.64 13.873 13.812 {'EVENT'} {'T.1116'} 6.2748e+05 1.5153e+05 -16.37 20 05-Dec-2022 03:15:19 1.3723 103.64 13.865 13.809 {'EVENT'} {'T.1117'} 6.2748e+05 1.5153e+05 -16.54 22 05-Dec-2022 03:15:21 1.3724 103.64 13.894 13.806 {'EVENT'} {'T.1118'} 6.2749e+05 1.5153e+05 -16.65 24 05-Dec-2022 03:15:23 1.3724 103.64 13.851 13.803 {'EVENT'} {'T.1119'} 6.2749e+05 1.5153e+05 -16.64 26 05-Dec-2022 03:15:25 1.3724 103.64 13.81 13.799 {'EVENT'} {'T.1120'} 6.275e+05 1.5152e+05 -16.79 28 05-Dec-2022 03:15:27 1.3724 103.64 13.784 13.796 {'EVENT'} {'T.1121'} 6.275e+05 1.5152e+05 -16.84 30 05-Dec-2022 03:15:29 1.3724 103.64 13.822 13.792 {'EVENT'} {'T.1122'} 6.2751e+05 1.5152e+05 -16.89 32 05-Dec-2022 03:15:31 1.3723 103.64 13.749 13.789 {'EVENT'} {'T.1123'} 6.2751e+05 1.5151e+05 -16.89

Categorías

Más información sobre Dates and Time en Help Center y File Exchange.

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by