MATLAB: Extract Variables from mixed string string Hi all, In this string '# Message: onset_pic1_8.png' how can I read 'onset', 'pic', '1', '8' in four variables? Best Answer S = '# Message: onset_pic1_8.png';parts = regexp(S, '(?<name1>[A-Za-z]+)_(?<name2>[A-Za-z]+)(?<num1>\d+)_(?<num2>\d+)', 'names')parts = struct with fields: name1: 'onset' name2: 'pic' num1: '1' num2: '8'I coded to permit uppercase as well as lowercase, but I did assume that the alphabetic parts remain alphabetic and the numeric parts remain numeric. Related SolutionsMATLAB: Exclude Digits from String using Regexp in MATLAB regexp(str,'[^\d_\.]*','match')orregexp(str,'[a-zA-Z]*','match') MATLAB: Regexprep() skip first occurrence How about using regexp to find the position of spaces, and delete 2nd~Nth spaces? Like:mystring = 'this is my string';pos = regexp(mystring,'\s');mystring(pos(2:end)) = []; Related QuestionLast Letter in a Text stringStrrep with replacing precise word and no ‘ ‘ explicit in codeExtract part of string using REGEXPDetecting the existence of alphabetical elementsSplit string into string based on character typeUsing different strings for same operations
Best Answer