필터 지우기
필터 지우기

How to build a" "for" cycle to concatenate strings

조회 수: 2 (최근 30일)
Hugo
Hugo 2022년 7월 12일
댓글: Walter Roberson 2022년 7월 13일
Hello,
I have a code that prints:
Var1=
Var2=
Var3=
The code is:
sa="VAR"
aaa="="
i1=num2str(1)
i2=num2str(2)
i3=num2str(3)
StringS1=[sa i1 aaa]
StringS2=[sa i2 aaa]
StringS3=[sa i3 aaa]
StringS=[StringS1;
StringS2;
StringS3]
However, I would like to build a code that considers any number of variables, defined previously, for example, in the variable nvar. I know this can be done using a for cycle, but I am not sure on how to implement string concatenation.
I thank you in advance,
Best regards,
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 7월 12일
편집: Walter Roberson 2022년 7월 12일
Notice that string() objects use double-quote
sa = "VAR"
sa = "VAR"
aaa = "="
aaa = "="
sa + aaa
ans = "VAR="
Hugo
Hugo 2022년 7월 12일
Thank you. I edited the post.

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

답변 (1개)

dpb
dpb 2022년 7월 12일
You're overthinking the problem --
>> N=3;
>> compose("VAR%d=",[1:N].')
ans =
3×1 string array
"VAR1="
"VAR2="
"VAR3="
>>
  댓글 수: 1
Walter Roberson
Walter Roberson 2022년 7월 13일
Or just
N = 3;
"Var" + (1:N).' + "="
ans = 3×1 string array
"Var1=" "Var2=" "Var3="

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by