Find the first matched string/value from a table
14 views (last 30 days)
Show older comments
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 Comments
Accepted Answer
Star Strider
on 13 Jun 2023
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 Comments
Star Strider
on 15 Jun 2023
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.
.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!