Matching datenum from multiple arrays
3 views (last 30 days)
Show older comments
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];
0 Comments
Answers (2)
Saurabh
on 10 Jun 2023
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.
0 Comments
Simon Chan
on 10 Jun 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
0 Comments
See Also
Categories
Find more on Dates and Time 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!