多次使用cellfun

m_p*_*wer 1 arrays matlab simulink

使用该命令get_param(maskBlock,'MaskVariables'),我得到一个如下所示的字符串:

'AB=@1;AC=@2;AD=@3;AE=@4;..AZ=@26;'
Run Code Online (Sandbox Code Playgroud)

我想更改数字并为它们添加1以获得:

'AB=@2;AC=@3;AD=@4;AE=@5;..AZ=@27;'
Run Code Online (Sandbox Code Playgroud)

在这里我编码:

strSplit = regexp(theStringFromGetParam , ';', 'split')'; % split the string at the ; to get multiple strings
str1 = cellfun(@(x) str2double(regexp(x,'(\d+)','tokens','once'))+1, strSplit, 'UniformOutput', false); % cell containing the last numbers
str2 = cellfun(@(x) regexp(x,'(\w+)(\W+)','tokens','once'), strSplit, 'UniformOutput', false); % cell containing everything that is not a number
str3 = cellfun(@(x) strcat(x{1}, x{2}), str2, 'UniformOutput', false); % join the two parts from the line above
str4 = cellfun(@(x,y) strcat(x,num2str(y)), str3, str1, 'UniformOutput', false); % join the number numbers with the "letters=@"
Run Code Online (Sandbox Code Playgroud)

它有效,但我几乎可以肯定有更好的方法来做到这一点.任何人都可以帮助我找到比使用4次命令更好的方法cellfun吗?

Ole*_*leg 6

这是一个单行:

str = 'AB=@1;AC=@2;AD=@3;AE=@4;AZ=@26;';
regexprep(str,'(?<=@)(\d+)','${sprintf(''%d'',str2double($1)+1)}')
Run Code Online (Sandbox Code Playgroud)

比赛是容易的:在字符串中的任何点,回望一个@,如果发现再搭配起一个或多个连续数字和捕捉到的令牌.

更换:str2double()捕获的标记,添加1和转换回一个整数.该命令使用动态表达式执行'${command}'.