Find the first matched string/value from a table
You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Show older comments
0 votes
Share a link to this question
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
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)
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 Comments
Thanks for the help first.
The command of 'DesiredResult = T1{idx(1),:}' is not working, and I think it is down to the fact that I didn't explain my application thoroughly.
Basically I import a table from an excel file by using the following command:
data = readtable('InputFile.xlsx','Sheet','RecordInfo','range','A:K');
Once I have got 'data' in my MATLAB workspace, I then search for the first matched 'Trenching' and get the row number. So I could read the relevant TypeNum cell content and read other cell contents (like Volt and Current) in the previous rows (like stand and stationary) (i.e. the table that I posted intially is only a small part of the data).
StepNum Activity TypeNum Volt Current
_______ ____________ _______ ____ _______
5098 "Stand" "1-96" 1.1 0.1
5099 "Stand" "1-96" 1.2 0.2
5101 "Stationary" "1-96" 1.3 0.3
5105 "Stationary" "1-96" 1.3 0.3
5106 "Stand" "1-97" 1.8 0.8
5107 "Stand" "1-97" 2 0
5113 "Stand" "1-97" 1.9 1.9
5114 "Trenching" "1-98" 8 79
5115 "Trenching" "1-98" 11.5 80.5
5116 "Trenching" "1-98" 24 80.2
Those retrived cell content values will be used in a calculation to produce the results like R = (Volt(5114) - Volt(5113)) / (Current(5114) - Current(5113)). The calculated R will be used to produce another table.
Hopefully it is making sense to you and you are able to give me some suggestion? Thanks a lot.
Regards,
Kane
Star Strider
on 14 Jun 2023
Attach the Excel file. Use the ‘paperclip’ icon in the top toolbar to do that.
My code returns the necessary information, however I had to create it from the text since the Excel file was not provided. In any event, to use the information my code in my answer returns, use the str2double function to retrieve the numeric results. This might not be necessary using the actual Excel file.
Kane Lok
on 15 Jun 2023
Moved: Star Strider
on 15 Jun 2023
Hi Star Strider,
The excel file attached and thanks for your help.
Regards,
Kane
Star Strider
on 15 Jun 2023
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.
.
More Answers (0)
Categories
Find more on Logical in Help Center and File Exchange
Tags
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)