Find the first matched string/value from a table

조회 수: 14 (최근 30일)
Kane Lok
Kane Lok 2023년 6월 13일
댓글: Star Strider 2023년 6월 15일
Hi,
I have got a table like below:
StepNum Activity TypeNum
5098 Stand 1-96
5099 Stand 1-96
5101 Stationary 1-96
5105 Stationary 1-96
5106 Stand 1-97
5107 Stand 1-97
5113 Stand 1-97
5114 Trenching 1-98
5115 Trenching 1-98
5116 Trenching 1-98
What I would like to do is to find the first matched of the string 'Trenching', then read the row/column number so that I can get the value/string in adjacent cells/rows. Just wondering if anyone here can help me out? Many thanks in advance.
Regards,
Kane

채택된 답변

Star Strider
Star Strider 2023년 6월 13일
One approach —
VN = {'StepNum' 'Activity' 'TypeNum'};
step = [5098
5099
5101
5105
5106
5107
5113
5114
5115
5116];
v23 = ["Stand" "1-96"
"Stand" "1-96"
"Stationary" "1-96"
"Stationary" "1-96"
"Stand" "1-97"
"Stand" "1-97"
"Stand" "1-97"
"Trenching" "1-98"
"Trenching" "1-98"
"Trenching" "1-98"];
T1 = table(step, v23(:,1), v23(:,2), 'VariableNames',VN)
T1 = 10×3 table
StepNum Activity TypeNum _______ ____________ _______ 5098 "Stand" "1-96" 5099 "Stand" "1-96" 5101 "Stationary" "1-96" 5105 "Stationary" "1-96" 5106 "Stand" "1-97" 5107 "Stand" "1-97" 5113 "Stand" "1-97" 5114 "Trenching" "1-98" 5115 "Trenching" "1-98" 5116 "Trenching" "1-98"
idx = find(ismember(T1{:,2}, "Trenching"));
DesiredResult = T1{idx(1),:} % Contents Of Row
DesiredResult = 1×3 string array
"5114" "Trenching" "1-98"
DesiredResult = T1(idx(1),:) % Sub-Table
DesiredResult = 1×3 table
StepNum Activity TypeNum _______ ___________ _______ 5114 "Trenching" "1-98"
.
  댓글 수: 4
Kane Lok
Kane Lok 2023년 6월 15일
이동: Star Strider 2023년 6월 15일
Hi Star Strider,
The excel file attached and thanks for your help.
Regards,
Kane
Star Strider
Star Strider 2023년 6월 15일
My pleasure!
Try this (including a calculation for ‘R’) —
T1 = readtable('activity_table.xlsx', 'VariableNamingRule','preserve')
T1 = 2307×9 table
SignNum StepName ActivityStepNum LoopNum StepNum Current(A) Voltage(V) Power(W) RelativeTime(d.h:min:s.ms) _______ _________ _______________ _______ _______ __________ __________ ________ __________________________ 1 {'Stand'} {'1-1'} 1 1 0 3.5252 0 {'0.00:01:00.000'} 2 {'Stand'} {'1-1'} 1 1 0 3.5252 0 {'0.00:02:00.000'} 3 {'Stand'} {'1-1'} 1 1 0 3.5252 0 {'0.00:03:00.000'} 4 {'Stand'} {'1-1'} 1 1 0 3.5253 0 {'0.00:04:00.000'} 5 {'Stand'} {'1-1'} 1 1 0 3.5253 0 {'0.00:05:00.000'} 6 {'Stand'} {'1-1'} 1 1 0 3.5252 0 {'0.00:06:00.000'} 7 {'Stand'} {'1-1'} 1 1 0 3.5253 0 {'0.00:07:00.000'} 8 {'Stand'} {'1-1'} 1 1 0 3.5253 0 {'0.00:08:00.000'} 9 {'Stand'} {'1-1'} 1 1 0 3.5252 0 {'0.00:09:00.000'} 10 {'Stand'} {'1-1'} 1 1 0 3.5252 0 {'0.00:10:00.000'} 11 {'Stand'} {'1-1'} 1 1 0 3.5253 0 {'0.00:11:00.000'} 12 {'Stand'} {'1-1'} 1 1 0 3.5253 0 {'0.00:12:00.000'} 13 {'Stand'} {'1-1'} 1 1 0 3.5253 0 {'0.00:13:00.000'} 14 {'Stand'} {'1-1'} 1 1 0 3.5253 0 {'0.00:14:00.000'} 15 {'Stand'} {'1-1'} 1 1 0 3.5253 0 {'0.00:15:00.000'} 16 {'Stand'} {'1-1'} 1 1 0 3.5252 0 {'0.00:16:00.000'}
VN = T1.Properties.VariableNames;
idx = find(ismember(T1{:,2}, "Trenching"))
idx = 1347×1
930 931 932 933 934 935 936 937 938 939
DesiredResult = T1(idx(1)+[-1 1],:) % Contents Of Row
DesiredResult = 2×9 table
SignNum StepName ActivityStepNum LoopNum StepNum Current(A) Voltage(V) Power(W) RelativeTime(d.h:min:s.ms) _______ _____________ _______________ _______ _______ __________ __________ ________ __________________________ 929 {'Stand' } {'1-3'} 1 3 0 4.19 0 {'0.01:00:00.000'} 931 {'Trenching'} {'1-4'} 1 4 -17.001 4.1828 -71.11 {'0.00:00:00.020'}
Volt = DesiredResult{:,ismember(VN,'Voltage(V)')}
Volt = 2×1
4.1900 4.1828
Current = DesiredResult{:,ismember(VN,'Current(A)')}
Current = 2×1
0 -17.0006
R = (Volt(2) - Volt(1)) / (Current(2) - Current(1))
R = 4.2351e-04
See if that does what you want.
.

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

추가 답변 (0개)

카테고리

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

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by