How to add strings to a list

조회 수: 19 (최근 30일)
Zaheer Bhojani
Zaheer Bhojani 2019년 10월 1일
편집: Adam Danz 2019년 10월 2일
How do I add strings to a list. I used if staements and used the functions append but its not working if one if statement is wrong. I dont know if I need to use a loop.
diagnosistitle = [];
x = 1;
y = 1;
z = 1;
if x == 1
str1 = append("X");
end
if y == 1
str2 = append("Y");
end
if z == 2
str3 = append("Z");
end
str = append(str1, str2, str3);
  댓글 수: 2
Adam Danz
Adam Danz 2019년 10월 1일
편집: Adam Danz 2019년 10월 2일
Could you provide examples of the strings you are concatenating?
str1 = 'first';
str2 = 'second';
str12 = [str1,str2]
% str12 =
% 'firstsecond'
Also, what's the point of "concatenating" a single string?
append("Z")
ans =
"Z"
the cyclist
the cyclist 2019년 10월 2일
It's probably important to distinguish between strings and character arrays here.

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

답변 (1개)

the cyclist
the cyclist 2019년 10월 2일
So, I'm not sure what are really trying to do. But the problem with your specific code is that because this if structure is not entered
if z == 2
str3 = append("Z");
end
then the variable str3 simply does not exist, when is why the line
str = append(str1, str2, str3);
fails.

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by