Find the first matched string/value from a table
조회 수: 14 (최근 30일)
이전 댓글 표시
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
댓글 수: 0
채택된 답변
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)
idx = find(ismember(T1{:,2}, "Trenching"));
DesiredResult = T1{idx(1),:} % Contents Of Row
DesiredResult = T1(idx(1),:) % Sub-Table
.
댓글 수: 4
Star Strider
2023년 6월 15일
My pleasure!
Try this (including a calculation for ‘R’) —
T1 = readtable('activity_table.xlsx', 'VariableNamingRule','preserve')
VN = T1.Properties.VariableNames;
idx = find(ismember(T1{:,2}, "Trenching"))
DesiredResult = T1(idx(1)+[-1 1],:) % Contents Of Row
Volt = DesiredResult{:,ismember(VN,'Voltage(V)')}
Current = DesiredResult{:,ismember(VN,'Current(A)')}
R = (Volt(2) - Volt(1)) / (Current(2) - Current(1))
See if that does what you want.
.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!