%% Split String Using Pattern as Delimiter
% Since R2020b
% Get the numbers from a string by treating text as a delimiter. Use a pattern to match the text. Then add up the numbers.
% First, create a string that has numbers in it.

str = "10 apples 3 bananas and 5 oranges"

%
% Then, create a pattern that matches a space character or letters.

pat = " " | lettersPattern

%
% Split the string using pat as the delimiter. The empty strings represent splits between spaces and sequences of letters that had nothing else between them. For example, in "10 apples", there is a split before the delimiter " ", and then between " " and "apples". Since there is nothing between the delimiters " " and "apples", the split function returns an empty string to indicate there is nothing between them.

N = split(str,pat)

%
% Discard the empty strings and keep the substrings that represent numbers.

N = N(strlength(N) > 0)

%
% Finally, convert N to a numeric array and sum over it.

N = str2double(N);
sum(N)

%
% For a list of functions that create pattern objects, see pattern.
% Copyright 2020 The MathWorks, Inc.