Finding a pattern in a table and the coordinates of the "first hit"

조회 수: 30 (최근 30일)
Andrew Sol
Andrew Sol 2024년 11월 14일 10:24
답변: Image Analyst 2024년 11월 14일 15:16
I have a table like this:
'NaN' NaN NaN
'NaN' NaN NaN
'NaN' NaN NaN
'07:02:58.830' 1 NaN
'07:02:58.830' 1 831
'07:02:58.830' 1 NaN
'07:02:58.830' 1 NaN
'07:02:58.830' 1 831
'NaN' NaN NaN
'07:02:59.430' 2 NaN
'07:02:59.430' 2 831
'07:02:59.430' 2 819
'07:02:59.430' 2 819
The first column contains the time in the format X:Y:Z.
What I need: I need to find this pattern in the first column and calculate the coordinates of the first hit.
What the result should look like: in the case of the table in question, the result should be the coordinate of the cell in which this pattern occurs for the first time. In this case, it is [row,col] = [4,1];

답변 (2개)

Sameer
Sameer 2024년 11월 14일 10:53
편집: Sameer 2024년 11월 14일 10:56
To find the first occurrence of a specific pattern in the first column of your table you can create a duration object from a time string ('07:02:58.830') and search for its first occurrence in table's first column using find
Here's how you can do it:
load('Table.mat');
pattern = duration('07:02:58.830', 'Format', 'hh:mm:ss.SSS');
firstColumn = Table{:, 1};
% Find the index of the first occurrence of the pattern
index = find(firstColumn == pattern, 1);
if ~isempty(index)
fprintf('The first occurrence of the pattern is at [row, col] = [%d, %d];\n', index, 1);
else
fprintf('Pattern not found.\n');
end
The first occurrence of the pattern is at [row, col] = [4, 1];
Please refer to below MathWorks documentation links:
Hope this helps!
  댓글 수: 2
Andrew Sol
Andrew Sol 2024년 11월 14일 11:04
편집: Andrew Sol 2024년 11월 14일 11:05
Thank you for your answer! And if the time is unknown in advance and you I to find a time pattern (the time can be any, the main thing is that it is in the format 'hh:mm:ss.SSS'), how will the code change?
In other words: how to find the first occurrence of an arbitrary time of a given format in a table?
Sameer
Sameer 2024년 11월 14일 11:57
load('Table.mat');
firstColumnStr = cellstr(Table{:, 1});
timePattern = '\d{2}:\d{2}:\d{2}\.\d{3}';
index = find(~cellfun('isempty', regexp(firstColumnStr, timePattern)), 1);
if ~isempty(index)
fprintf('The first occurrence of the time pattern is at [row, col] = [%d, %d];\n', index, 1);
else
fprintf('Time pattern not found.\n');
end
The first occurrence of the time pattern is at [row, col] = [4, 1];

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2024년 11월 14일 15:16
Another way is to use ismember
% Load data from .mat file.
S = load('Andrew Table.mat');
t = S.Table;
% Extract first column into it's own variable (optional - just for convenience).
times = t{:, 1}; % This variable is of type "duration".
% Define the pattern we are trying to find
pattern = duration([07,02,58.830])
pattern = duration
07:02:58
% Find the first row where it occurs
[~, matchingRow] = ismember(pattern, times)
matchingRow = 4

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by