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!