Find the first matched string/value from a table
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
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
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
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
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
2023년 6월 15일
이동: Star Strider
2023년 6월 15일
Hi Star Strider,
The excel file attached and thanks for your help.
Regards,
Kane
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개)
카테고리
도움말 센터 및 File Exchange에서 Logical에 대해 자세히 알아보기
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- 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)
