Concatenate string output to another string vertically in a loop

조회 수: 10 (최근 30일)
as hz
as hz 2012년 10월 16일
Hi,
I have a loop which generate for every run a string output (loop size index is changing depends on how much files are in the directory). How can I concatenate the string output to another string vertically/send them into char/etc so I can save the final output (all the string output results) to a one text file?
Any idea is welcome.
Thanks.

답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 10월 16일
s=[]
for k=1:10
s1=['text' num2str(k)]
s=strvcat(s,s1)
end
  댓글 수: 1
Matt J
Matt J 2012년 10월 16일
편집: Matt J 2012년 10월 16일
Note - strvcat will eventually be removed from MATLAB. It is recommended in MATLAB documentation to use char instead. Also, it would be better to perform the concatenation after the loop (storing the strings in cells) so that s doesn't grow iteratively.

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


Matt J
Matt J 2012년 10월 16일
Assuming you've read the strings into a cell array you can use CHAR to concatenate them, e.g.,
>> char({'car','apple'})
ans =
car
apple
  댓글 수: 2
as hz
as hz 2012년 10월 16일
But it will only save the last string, not all of them-
Matt J
Matt J 2012년 10월 16일
편집: Matt J 2012년 10월 16일
Clarify what part of your code you're talking about (and show it). You should be able to see in my example that char({'car','apple'}) gave you both strings in its output, not just the last one.

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

카테고리

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