function [F] = aux_extract_numbers(A,delim) %%% extracting individual numbers from string "A" with numerical entries %%% separated by delimiters "delim" made up of a SINGLE character; %%% multiple delimiters are possible, for example '73&58&49992&11'; %%% NOTE: the strings shouldn't have blank spaces and/or consecutive %%% characters like "--", and should start and end with a number! See the %%% example above %%% output: vector "F" of the individual numbers % % Dmitry Yumashev, 15/04/2019 % Dmitry Yumashev, 14/06/2019 % delim = string(delim); % making sure the delimiter is in the right format % first, use string conversion to handle purely numeric entries B = string(A); % now, convert the string into character array, to be split into the individual % characters later C = char(B); totalCharC=length(C); % count the number of delimiters k = 0; for j=1:totalCharC if strcmp(C(j),delim) == 1 k = k + 1; end end totalDelimC = k; % the quantity of numbers separated by '&' or other delimeters totalNumC = totalDelimC + 1; % assuming the input string starts and ends with a number % extract the individual numbers into elements of a cell array D = cell(1,totalNumC); k = 0; jOld = 1; for j=1:totalCharC if strcmp(C(j),delim) == 1 k = k + 1; % counter jNew = j-1; % last character before the current '&' D{k} = C(jOld:jNew); jOld = j+1; % update the old character position end end % extracting the final number (not done in the loop above) jNew = totalCharC; D{totalNumC} = C(jOld:jNew); % converting strings into numbers F = str2double(D); % closing the function end