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 个评论
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
2023-11-7
编辑:Stephen23
2023-11-7
Fake data:
dt = datetime(2023,11,1:7).';
V = rand(7,1);
T = table(dt,V)
Date that you want:
want = datetime(2023,11,4)
Obtain data:
X = T.dt==want;
Z = T{X,'V'}
2 个评论
Peter Perkins
2023-11-10
Braces are fine, but dot is where I gravitate to for only one variable:
Z = T.V(T.dt==want)
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
2023-11-7
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
Query = "02-Jan-2023 09:00:00"
Result = WaveTable(WaveTable.DateTime == datetime(Query), :)
figure
plot(WaveTable.DateTime, WaveTable.Waves)
grid
xline(datetime(Query), '-r')
.
0 个评论
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)
If I want the entry for December 18th, 2015 at 10:03 (to within say +/- 30 seconds):
target = datetime('2015-12-18 10:03')
rangeToSearch = timerange(target-seconds(30), target+seconds(30), "closed")
TT(rangeToSearch, :)
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!