trying to find which row this specific date time is at in an excel sheet
13 次查看(过去 30 天)
显示 更早的评论
hi,
i have two excel sheets that have been read into matlab. im trying to find out what row number has the same datetime in excel2 from excel1.
NumRows = size(data1,1); ----> i have found the last row from excel 1
ValueAtNumRow = data1.TestTime(NumRows,1) -----> found the datetime value from excel2
[row, col] = find(P3.Time(end) - ValueAtNumRow) ----> now i want to find what row has this value in excel2
please help. im still learning and have gotten a lot of errors so far.
thank you!
0 个评论
回答(2 个)
Star Strider
2023-9-14
编辑:Star Strider
2023-9-14
ValueAtNumRow = datetime('11-Sep-2023 22:25:26', 'InputFormat','dd-MMM-yyyy HH:mm:ss')
dt1 = datetime('11-Sep-2023 22:25:10', 'InputFormat','dd-MMM-yyyy HH:mm:ss') + seconds(1:20).'
Lv = ismember (dt1, ValueAtNumRow);
Result = dt1(Lv)
RowNr = find(Lv) % Desired Result
EDIT — (14 Sep 2023 at 21:58)
It would help to have your files. My code should work if there is a match between the dates.
Any approach that creates a logical vector result will produce zeros for the entries that do not match. You need to test only the datetime variables — not the entire file — in both files, so using find with two outputs is probably not appropriate if you only want to find the row number.
.
dpb
2023-9-14
Presuming (although you didn't tell/show us) from the "dot" notation you used readtable to load the data into MATLAB,
NumRows=height(data1); % the last row from excel 1 -- tables have a more convenient function than using size()
ix=(data2.TestTime==data1.TestTime(NumRows)); % logical vector of matching indices (rows) in second
Now use ix to retrieve the rows of interest from the second table.
Given both have time data, you should consider using a timetable instead of a plain table; it has many additional features specifically builtiin/available for handling time-related processing that could be very helpful.
Also, just as syntax and shortcut in MATLAB, the above could also be written as
ix=(data2.TestTime==data1.TestTime(end));
using the builtin. More on <indexing expression> is at the link; time invested in going thru the early syntax sections of the doc or the online OnRamp tutorial will pay back the time invested multiple times over in time (and frustration) saved.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Spreadsheets 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!