How to know exactly what row (or index number?) in a table based on user input?
10 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Dyuman Joshi
le 7 Nov 2023
"... 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
Réponses (3)
Stephen23
le 7 Nov 2023
Modifié(e) : Stephen23
le 7 Nov 2023
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 commentaires
Peter Perkins
le 10 Nov 2023
Braces are fine, but dot is where I gravitate to for only one variable:
Z = T.V(T.dt==want)
Stephen23
le 11 Nov 2023
"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
le 7 Nov 2023
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 commentaires
Steven Lord
le 7 Nov 2023
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 commentaires
Voir également
Catégories
En savoir plus sur Dates and Time 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!