Plot of head against velocity data based on timestamp similarity

Hi,
I have 2 excel files named TimeStamps_Uy.xlsx and Head_Timestamps.xlsx. In file one: TimeStamps_Uy.xlsx column 1 have a Timestamps and column 2 have the velocity data. In file two: column 1 have a Timestamps and column 2 have the Head data. I want to use TimeStamps_Uy.xlsx file and all of its Timestamps to fine the similar Timestamps in the Head_Timestamps.xlsx hence on matching plot the corresponding data (head/Uy) from both files column 2 for the same or near enough time stamp hence save the timestamp, head, and Uy into new xlsx or csv file. How would I do that? I have attached both the xlsx file in this post. I have tried but getting nowhere! Code below is just show the path and normal reading of data from column.
Code so far:
Uy_dir = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\5-AverageFilter_VelocitiesExtration\Point_Velocities\Uy\TimeStamps_Uy.xlsx';
a = readmatrix(Uy_dir);
x=a(:,1);
y=a(:,2);
Head_dir = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_Head\Data_Raw\Head_Timestamps.xlsx';
a = readmatrix(Head_dir);
x=a(:,1);
y=a(:,2);

 Accepted Answer

First read in both of your datafiles.
Then use MATLAB function interp1 to interpolate values at times matching the times in one of the data files (doesn't matter which one)
So suppose that you have vectors tu, u for times and corresponding velocities and also th, h for times and corresponding head values. Then you could interpolate values of head at times matching those in the velocity data using
hq = interp1(th,h,tu)
and then if you wanted to plot head vs velocity you could plot the interpolated head values against the velocity values
plot(u,hq)

7 Comments

Hi, Thanks for the help first of all. I tried it but my column 1 became Nann and got nothing on the graph.
Code:
close all; clear all; clc;
Uy_dir = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\5-AverageFilter_VelocitiesExtration\Point_Velocities\Uy\TimeStamps_Uy.xlsx';
a = readmatrix(Uy_dir);
u=a(:,1);
tu=a(:,2);
Head_dir = 'F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_Head\Data_Raw\Head_Timestamps.xlsx';
a1 = readmatrix(Head_dir);
h=a(:,1);
th=a(:,2);
hq = interp1(th,h,tu)
plot(u,hq)
Column 1 before running a code:
13:31:39 0
13:31:49 8.1
13:31:59 15.3
13:32:09 24.4
13:32:19 30.8
13:32:29 38.3
13:32:39 44
13:32:49 50.4
13:32:59 57.6
13:33:09 63.4
13:33:19 68.7
13:33:29 74.1
13:33:39 78.9
13:33:49 83.5
13:33:59 88.3
13:34:09 93.1
13:34:19 97.4
13:34:29 102.1
13:34:39 105.7
13:34:49 109.6
13:34:59 113.6
13:35:09 117
column 1 after running the code:
NaN 0
NaN 8.10000000000000
NaN 15.3000000000000
NaN 24.4000000000000
NaN 30.8000000000000
NaN 38.3000000000000
NaN 44
NaN 50.4000000000000
NaN 57.6000000000000
NaN 63.4000000000000
NaN 68.7000000000000
NaN 74.1000000000000
NaN 78.9000000000000
NaN 83.5000000000000
NaN 88.3000000000000
NaN 93.1000000000000
readmatrix won't read in mixed data types, e.g. first column is time stamp and second column is numeric. So it gives NaN for the times. Instead try using readtable or readtimetable instead. So for example:
% read data into tables
Udat = readtable('TimeStamps_Uy.xlsx');
Hdat = readtable('Head_Timestamps (1).xlsx');
% assign variables
tu = datetime(Udat.TimeStamp); % convert to datetime array
u = Udat.Uy
th = datetime(Hdat.TimeStamp);% convert to datetime array
h = Hdat.Head_mm_; % Matlab converts original heading Head(mm) to this legal variable name
% interpolate values for head at times to match those where we have
% velocity data
hq = interp1(th,h,tu);
% plot the results
plot(u,hq,'o')
By the way, doing this, it doesn't look like you have much correlation between your velocity and head values. Maybe there is some other issue in your experiment, or data gathering.
Thanks alot for this! How can I find out what timestamps we used for the u,hq. is there any way to find out? Yes, the data selection were bit messed up at the start but once I have rough code made, I will reran on the raw data.
The time stamps for hq (the query points of the interpolation) are the same as the timestamps for u, that is tu. Please have a look at the documentation for interp1. What you have done is to use the given th (timestamps for h) and values for h, and interpolated new values hq, at the query points tu.
Hope this helps. If this answered your question, please remember to accept the answer so that others with a similar problem can find it.
Thanks alot for the explaination, yes I have accepted the answer. Thanks again!
Great, good luck with your experiment.

Sign in to comment.

More Answers (0)

Asked:

on 10 Mar 2022

Commented:

Jon
on 10 Mar 2022

Community Treasure Hunt

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

Start Hunting!