필터 지우기
필터 지우기

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

조회 수: 16 (최근 30일)
Thibaut
Thibaut 2020년 6월 7일
댓글: Ameer Hamza 2020년 6월 8일
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
madhan ravi
madhan ravi 2020년 6월 7일
Thibaut
Thibaut 2020년 6월 7일
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 !

댓글을 달려면 로그인하십시오.

채택된 답변

Ameer Hamza
Ameer Hamza 2020년 6월 7일
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)'
  댓글 수: 2
Thibaut
Thibaut 2020년 6월 7일
Thank you really much !
Ameer Hamza
Ameer Hamza 2020년 6월 8일
I am glad to be of help!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by