MATLAB: Convert string of hex to array of dec

hexparse string

I need to convert a string of hexidecimal numbers to an array of dec numbers. The string can potentially contain incomplete hex numbers at the beginning and end of the string. See examples below. I'm trying to use this in a data acquisition setup…so execution time could be a factor.
Input String Examples ('/r' = carriage returns:
s1 = '0001h/r0002h/r0003h/r';
s2 = '4h/r0001h/r0002h/r0003h/r00';
Output Desired:
a1 = [1,2,3]
a2 = [1,2,3]

Best Answer

s1 = '0001h/r0002h/r0003h/r';
s1 = '4h/r0001h/r0002h/r0003h/r00';
str=regexp(s1,'h/r','split');
Ind=cellfun(@length,str);
str=str(Ind==4);
num=hex2dec(str);