필터 지우기
필터 지우기

Extract Numbers from Mixed string

조회 수: 14 (최근 30일)
Jason
Jason 2024년 2월 9일
댓글: Jason 2024년 2월 9일
Hello, I have this string
'single snap detector: 1 S2L:232867 S2R:3151621 S3L:0 S3R:0'
Whats the best way to get the 4 numbers and then the S2R - S2L value?
I have tried this:
B='single snap detector: 1 S2L:232867 S2R:3151621 S3L:0 S3R:0'
pat=regexpPattern("S2L:[/d]")
extract(B,pat)
But Im getting an empty array
  댓글 수: 1
Jason
Jason 2024년 2월 9일
Actually, this seems to work not sure if its th e best way:
sscanf(B,'single snap detector: 1 S2L:%d S2R:%d S3L:%d S3R:%d')
ans =
232867
3151621
0
0

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

채택된 답변

Hassaan
Hassaan 2024년 2월 9일
% Your input string
B = 'single snap detector: 1 S2L:232867 S2R:3151621 S3L:0 S3R:0';
% Regular expression pattern to match the labels and their corresponding values
pattern = 'S2L:(\d+) S2R:(\d+) S3L:(\d+) S3R:(\d+)';
% Using regexp to find the matches and extract the numbers
tokens = regexp(B, pattern, 'tokens');
% tokens{1} will contain a cell array of the matched groups if a match is found
if ~isempty(tokens)
% Converting the extracted strings to numbers
numbers = str2double(tokens{1});
S2L = numbers(1);
S2R = numbers(2);
S3L = numbers(3);
S3R = numbers(4);
% Calculate S2R - S2L
difference = S2R - S2L;
else
S2L = NaN;
S2R = NaN;
S3L = NaN;
S3R = NaN;
difference = NaN;
end
% Displaying the results
S2L, S2R, S3L, S3R, difference
S2L = 232867
S2R = 3151621
S3L = 0
S3R = 0
difference = 2918754
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
A multiverse of answers and solutions.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

추가 답변 (3개)

Vaibhav
Vaibhav 2024년 2월 9일
편집: Vaibhav 2024년 2월 9일
Hi Jason
You can consider using the below regular expression to extract the required values.
'S2L:(\d+) S2R:(\d+) S3L:(\d+) S3R:(\d+)'
Hope this helps!

Stephen23
Stephen23 2024년 2월 9일
편집: Stephen23 2024년 2월 9일
B = 'single snap detector: 1 S2L:232867 S2R:3151621 S3L:0 S3R:0';
Method one: SSCANF:
V = sscanf(B,'%*[^:]:%d') % very efficient
V = 5×1
1 232867 3151621 0 0
Method two: REGEXP (could also be EXTRACT):
T = regexp(B,'(S\w+):(\d+)','tokens');
T = vertcat(T{:}) % all relevant parts
T = 4×2 cell array
{'S2L'} {'232867' } {'S2R'} {'3151621'} {'S3L'} {'0' } {'S3R'} {'0' }
V = str2double(T(:,2))
V = 4×1
232867 3151621 0 0
  댓글 수: 3
Stephen23
Stephen23 2024년 2월 9일
@Jason: if you need only the numeric values and want efficient code then you won't get faster than the SSCANF call I showed you.
Jason
Jason 2024년 2월 9일
Yeah thats what Im actually using :-), thankyou!

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


Mathieu NOE
Mathieu NOE 2024년 2월 9일
hello
i supposed you wanted to do this :
B='single snap detector: 1 S2L:232867 S2R:3151621 S3L:0 S3R:0'
B = 'single snap detector: 1 S2L:232867 S2R:3151621 S3L:0 S3R:0'
% pat=regexpPattern("S2L:[/d]")
pat=regexpPattern("S2L:[\d]")
pat = pattern
Matching: regexpPattern("S2L:[\d]")
extract(B,pat)
ans = 1×1 cell array
{'S2L:2'}
my2 cents suggestion to extract all numbers present in this char array (up to you to pick the ones you are intersted in);
B='single snap detector: 1 S2L:232867 S2R:3151621 S3L:0 S3R:0'
B = 'single snap detector: 1 S2L:232867 S2R:3151621 S3L:0 S3R:0'
C = regexp(B,'\d+(\.)?(\d+)?','match');
% convert string to num array
for ci = 1:numel(C)
out(ci,:) = str2double(C{ci});
end
out
out = 9×1
1 2 232867 2 3151621 3 0 3 0
  댓글 수: 1
Jason
Jason 2024년 2월 9일
Thnakyou for pointing out my error!

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

카테고리

Help CenterFile Exchange에서 Introduction to Installation and Licensing에 대해 자세히 알아보기

태그

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by