How to know exactly what row (or index number?) in a table based on user input?

10 次查看(过去 30 天)
Hello. I have a table of data that is two columns and about 9.5 million rows. The first column is datetime values and the second column is just scalar numbers. What would the syntax be so that I can have the user input a specific date, which then corresponds to a row in the data so then I can know the data in the second column at that specific date. Thanks!
  3 个评论
Wilson Meireles
Wilson Meireles 2023-11-7
Maybe I wasnt exacly clear in my question. I know how to get the user to input the date as a text and then convert to date time (thank you for the reassurance that I was doing that right) but then can I have that user input essentially "seach" the table for the same date and then something in the code tells me which row of the table thats on? rather than manually searching for the matching date
Dyuman Joshi
Dyuman Joshi 2023-11-7
"... then something in the code tells me which row of the table thats on?"
See the last 2 lines of code in Stephen's answer below.
If you want to get the index/indices of the rows, use find

请先登录,再进行评论。

回答(3 个)

Stephen23
Stephen23 2023-11-7
编辑:Stephen23 2023-11-7
Fake data:
dt = datetime(2023,11,1:7).';
V = rand(7,1);
T = table(dt,V)
T = 7×2 table
dt V ___________ ________ 01-Nov-2023 0.99311 02-Nov-2023 0.94944 03-Nov-2023 0.023913 04-Nov-2023 0.37734 05-Nov-2023 0.55822 06-Nov-2023 0.51221 07-Nov-2023 0.20327
Date that you want:
want = datetime(2023,11,4)
want = datetime
04-Nov-2023
Obtain data:
X = T.dt==want;
Z = T{X,'V'}
Z = 0.3773
  2 个评论
Stephen23
Stephen23 2023-11-11
"Braces are fine, but dot is where I gravitate to for only one variable"
In absence of such information from the OP I prefer to provide an answer that works for all column/variable names.

请先登录,再进行评论。


Star Strider
Star Strider 2023-11-7
The file posted in your previous Question had only time values.
Assuming the data you refer to here is different, the matching value would also have to be a datetime value —
DateTime = datetime(2023,1,1) + hours(0:71).';
Waves = sin(2*pi*(0:numel(DateTime)-1).'/24) + randn(size(DateTime))/10 + 10;
WaveTable = table(DateTime,Waves) % Create Data Table
WaveTable = 72×2 table
DateTime Waves ____________________ ______ 01-Jan-2023 00:00:00 9.9743 01-Jan-2023 01:00:00 10.451 01-Jan-2023 02:00:00 10.632 01-Jan-2023 03:00:00 10.659 01-Jan-2023 04:00:00 10.787 01-Jan-2023 05:00:00 10.979 01-Jan-2023 06:00:00 11.085 01-Jan-2023 07:00:00 10.759 01-Jan-2023 08:00:00 10.982 01-Jan-2023 09:00:00 10.637 01-Jan-2023 10:00:00 10.413 01-Jan-2023 11:00:00 10.184 01-Jan-2023 12:00:00 9.9066 01-Jan-2023 13:00:00 9.8177 01-Jan-2023 14:00:00 9.5956 01-Jan-2023 15:00:00 9.2359
Query = "02-Jan-2023 09:00:00"
Query = "02-Jan-2023 09:00:00"
Result = WaveTable(WaveTable.DateTime == datetime(Query), :)
Result = 1×2 table
DateTime Waves ____________________ ______ 02-Jan-2023 09:00:00 10.638
figure
plot(WaveTable.DateTime, WaveTable.Waves)
grid
xline(datetime(Query), '-r')
.

Steven Lord
Steven Lord 2023-11-7
If you had a timetable array, you could either use the == operator as others have suggested or create a timerange (if you need to find rows with times that are "close enough" to the specified time but not exactly equal.) Using the sample timetable from the documentation:
MeasurementTime = datetime({'2015-12-18 08:03:05';'2015-12-18 10:03:17';'2015-12-18 12:03:13'});
Temp = [37.3;39.1;42.3];
Pressure = [30.1;30.03;29.9];
WindSpeed = [13.4;6.5;7.3];
WindDirection = categorical({'NW';'N';'NW'});
TT = timetable(MeasurementTime,Temp,Pressure,WindSpeed,WindDirection)
TT = 3×4 timetable
MeasurementTime Temp Pressure WindSpeed WindDirection ____________________ ____ ________ _________ _____________ 18-Dec-2015 08:03:05 37.3 30.1 13.4 NW 18-Dec-2015 10:03:17 39.1 30.03 6.5 N 18-Dec-2015 12:03:13 42.3 29.9 7.3 NW
If I want the entry for December 18th, 2015 at 10:03 (to within say +/- 30 seconds):
target = datetime('2015-12-18 10:03')
target = datetime
18-Dec-2015 10:03:00
rangeToSearch = timerange(target-seconds(30), target+seconds(30), "closed")
rangeToSearch =
timetable timerange subscript: Select timetable rows with times in the closed interval: [18-Dec-2015 10:02:30, 18-Dec-2015 10:03:30]
TT(rangeToSearch, :)
ans = 1×4 timetable
MeasurementTime Temp Pressure WindSpeed WindDirection ____________________ ____ ________ _________ _____________ 18-Dec-2015 10:03:17 39.1 30.03 6.5 N
If you want to convert your table to a timetable to use timerange (and the other timetable-specific functionality like retime and synchronize), use table2timetable.

类别

Help CenterFile Exchange 中查找有关 Dates and Time 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by