How to "increment" a pattern in a string array ?

22 views (last 30 days)
Hello guys,
I have a string character like : '[XYZ]_(?:%d|%d|%d|%d)' .In this example I have 4 values (%d) but maybe in the future I could have more. So I would like to create a loop to increment the pattern automatically.
For instance, if tomorrow I have 5 values, I would like to have :
[XYZ]_(?:%d|%d|%d|%d|%d)
Is there anyway to do this ?
Thanks you really much for your help !
Thibaut
  2 Comments
Thibaut
Thibaut on 7 Jun 2020
In some applications, I have only 5 variables but in some others, I can have up to 19 variables. It means I have to increment the pattern X times by myself which is really long and source of mistake !

Sign in to comment.

Accepted Answer

Ameer Hamza
Ameer Hamza on 7 Jun 2020
Something like this
x = 1:4;
sprintf(['[XYZ]_(?:' repmat('%d|',1,numel(x)-1) '%d)'], x)
Result
ans =
'[XYZ]_(?:1|2|3|4)'
and
x = 1:10;
sprintf(['[XYZ]_(?:' repmat('%d|',1,numel(x)-1) '%d)'], x)
Result
ans =
'[XYZ]_(?:1|2|3|4|5|6|7|8|9|10)'

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!