How to extract number from this string cell?

72 views (last 30 days)
Hi everyone,
I have a string array like below:
") injection-dr0015:138751)"
") injection-dr0015:68761)"
") injection-dr0015:122762)"
") injection-dr0015:83759)"
") injection-dr0015:163753)"
I would like to extract the numbers after : , like:
138751
68761
122762
83759
163753
I appreciate for any ideas or solution.
Thank you.

Accepted Answer

Star Strider
Star Strider on 16 Oct 2022
Try something like this —
C = [") injection-dr0015:138751)"
") injection-dr0015:68761)"
") injection-dr0015:122762)"
") injection-dr0015:83759)"
") injection-dr0015:163753)"];
E = str2double(extractBetween(C, ":",")"))
E = 5×1
138751 68761 122762 83759 163753
.

More Answers (1)

Image Analyst
Image Analyst on 16 Oct 2022
Try this (one of several ways):
str = [") injection-dr0015:138751)"
") injection-dr0015:68761)"
") injection-dr0015:122762)"
") injection-dr0015:83759)"
") injection-dr0015:163753)"];
strNumbers = cell(numel(str), 1); % Use this if you want as character arrays
dblNumbers = zeros(numel(str), 1); % Use this if you want double numbers
for k = 1 : numel(str)
thisString = char(str(k));
index = strfind(thisString, ':') + 1;
% Use this if you want as character arrays:
strNumbers{k} = thisString(index : end-1);
% Use this if you want double numbers.
dblNumbers(k) = str2double(strNumbers{k});
end
strNumbers
strNumbers = 5×1 cell array
{'138751'} {'68761' } {'122762'} {'83759' } {'163753'}
dblNumbers
dblNumbers = 5×1
138751 68761 122762 83759 163753

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!