Countering error while using interp1 function

4 vues (au cours des 30 derniers jours)
muhammad choudhry
muhammad choudhry le 13 Mar 2022
Commenté : Peter Perkins le 14 Mar 2022
Hi,
I am using the code below but getting the error and can not make sense of that error can anyone help please: Attached are the files I am working with.Basically, I am trying to use the time stamps available in both files attached and find the common timestamps for both velocity and head data hence plot them for visualization but I think the size of the both files are different that's why error occuring?
Code:
close all; clear all; clc;
% read data into tables
Udat = readtable('F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_PIV\5-AverageFilter_VelocitiesExtration\Point_Velocities\Uy\TimeStamps_Uy.xlsx');
Hdat = readtable('F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_Head\Data_Raw\Head_Timestamps.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')
Error:
Error using datetime (line 597)
Numeric input data must be a matrix with three or six columns, or else three, six, or seven separate numeric arrays. You can also create datetimes from a
single numeric array using the 'ConvertFrom' parameter.
Error in headvelocitygraph (line 10)
th = datetime(Hdat.TimeStamp);% convert to datetime array

Réponse acceptée

John D'Errico
John D'Errico le 13 Mar 2022
Modifié(e) : John D'Errico le 13 Mar 2022
Your problem is NOT in interp1. It is in the incorrect use of datetime. Read the error message:
Error using datetime (line 597)
Numeric input data must be a matrix with three or six columns, or else three, six, or seven separate numeric arrays. You can also create datetimes from a
single numeric array using the 'ConvertFrom' parameter.
  3 commentaires
muhammad choudhry
muhammad choudhry le 13 Mar 2022
see why I am not using the same error using the TimeStamps_Uy.xlsx, this file running fine but as soon as code come the file Head_Timestamps.xlsx. it start generating error, both files have the extracted data from the sensors, is it means both are transfering the data in different format?
I read the error it says first change the readtable to read matrix ones I done that it gave me error below,
Dot indexing is not supported for variables of this type.
Error in headvelocitygraph (line 10)
th = datetime(Hdat.TimeStamp,'InputFormat','dd.MM.yyyy HH:mm:ss.SSS');% convert to datetime array
then I used one more line to convert the below line into datetime format but still getting error
th = datetime(Hdat.TimeStamp,'InputFormat','dd.MM.yyyy HH:mm:ss.SSS');% convert to datetime array
Peter Perkins
Peter Perkins le 14 Mar 2022
These two files have what appears to be times of day, or more generally, elapsed times. But the two files are apparently formatted completely differently, because readtable reads the timestamps in one as numeric, in the other as text. Neither is ideal; a properly formatted spreadsheet should cause readtable to read them in as timestamps. Anyway:
>> t1 = readtable("Head_Timestamps.xlsx");
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
>> head(t1)
TimeStamp Head_mm_
_________ ________
0.56369 0
0.5637 1.2
0.56372 1.2
0.56373 0.7
0.56374 0.8
[snip]
>> t2 = readtable("TimeStamps_Uy.xlsx");
>> head(t2)
TimeStamp Uy
____________ _________
{'13:32:04'} 0.0023132
{'13:33:09'} 0.068063
{'13:33:42'} 0.056888
{'13:34:14'} 0.070543
{'13:34:46'} 0.13177
[snip]
>> t1.TimeStamp = days(t1.TimeStamp); t1.TimeStamp.Format = "hh:mm:ss.SSS";
TimeStamp Head_mm_
____________ ________
13:31:43.000 0
13:31:44.000 1.2
13:31:45.000 1.2
13:31:45.999 0.7
13:31:47.000 0.8
[snip]
>> t2.TimeStamp = duration(t2.TimeStamp); t2.TimeStamp.Format = "hh:mm:ss.SSS"
t2 =
45×2 table
TimeStamp Uy
____________ ___________
13:32:04.000 0.0023132
13:33:09.000 0.068063
13:33:42.000 0.056888
13:34:14.000 0.070543
13:34:46.000 0.13177
[snip]

Connectez-vous pour commenter.

Plus de réponses (1)

Walter Roberson
Walter Roberson le 13 Mar 2022
th = datetime(Hdat.TimeStamp);% convert to datetime array
You did not use any options when you readtable(): how can you be sure that TimeStamp was read in as either string array or cell array of character vectors? How do you know that the format was not recognized and it might already have been converted to datetime? Or perhaps the column got misidentified as numeric?
  7 commentaires
muhammad choudhry
muhammad choudhry le 14 Mar 2022
You are right Peter, it was durations, but i finally figured that out yesterday by using the class() and updated the code accordingly as shown below. One question since your reply here! how would I know by just looking at the data rather its datetime, or duration ? if the data come out as 12 March 2012 13:03:01 or just 13:03:01. I spent all day yesterday just to understand this
Updated Code:
% read data into tables
Udat = readtable('F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_Head\Data_Raw\Head_TimestampsCSV.csv');
Hdat = readtable('F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_Head\Data_Raw\Head_TimestampsCSV.csv');
%Check the data type before proceeding with the different data =>
u = Udat.Uy
h = Hdat.Head_mm_; % Matlab converts original heading Head(mm) to this legal variable name
tu=duration(Udat.TimeStamp_Uy);
th = duration(Hdat.TimeStamp);
% 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')
size(h)
size(Hdat)
size(hq)
size(th)
Peter Perkins
Peter Perkins le 14 Mar 2022
I can't answer that. You need to have at least some idea what is in your data files. datetime assumes that if you give it a partial (time-only) timestamp, you mean "today". In my opinion, your data files are the culprit and are not formatted properly.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by