Matching datenum from multiple arrays
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuestas (1)
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
0 comentarios
Ver también
Categorías
Más información sobre Dates and Time en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!