필터 지우기
필터 지우기

Adding a space into an array of string

조회 수: 8 (최근 30일)
Jiwon Park
Jiwon Park 2022년 4월 14일
댓글: Stephen23 2022년 4월 14일
words={'Everything';'is';'theoretically';'impossible,';'until';'it';'is';'done.'};
[rownum,~]=size(words);
words_array=char(words);
[~,colnum]=size(words_array);
sentence=strings(1);
TF=ismissing(words_array);
i=1;
j=1;
while j<colnum+1 && i<rownum+1
temp=words_array(i,j);
sentence=sentence+temp;
if TF(i,j)==1
i=i+1;
j=1;
elseif j+1>colnum
i=i+1;
j=1;
else
j=j+1;
end
end
I have to create an string array that puts the words provided together into a string array.
So expected output of this code is
'Everything is theoretically impossible, until it is done.'
In a 1X1 string array.(here I have used the variable 'sentence')
However, this code does not provide spacing between the words for the elseif statement.
The current output of the code above is
'Everything is theoreticallyimpossible, untill it is done.'
I need to add a space into the variable sentence when my else if statement holds true.
So far, I have tried
elseif j+1>colnum
i=i+1;
j=1;
sentence=sentence+'';
But this does not work.
How do I add a space to the variable sentence under else if statement?
  댓글 수: 1
Stephen23
Stephen23 2022년 4월 14일
words = {'Everything';'is';'theoretically';'impossible,';'until';'it';'is';'done.'};
out = join(string(words))
out = "Everything is theoretically impossible, until it is done."

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

답변 (1개)

Walter Roberson
Walter Roberson 2022년 4월 14일
sentence=sentence+" ";

카테고리

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