필터 지우기
필터 지우기

I am struggling to use sprint

조회 수: 5 (최근 30일)
Angus Wong
Angus Wong 2019년 4월 29일
댓글: Angus Wong 2019년 4월 29일
Hi,
I have a problem regarding the use of sprintf.
Inside a user-defined function (which I can edit), the output is "A" and it is defined as:
A=sprintf('The fruits are %s and they are tasty.\n',f); waitfor(msgbox(A));
where f is defined as a string vector with 1 row. The number of columns varies between 1 to 100. For each element, there is a comma at the end of the string.
f is the input of the UDF, and I have no control of the length of it. f comes from another file that I have no control of. f always follows the same pattern, as shown as bolow.
Here are some examples:
1. f=["apple," "pear," "banana,"];
The ideal output is: The fruits are apple,pear,banana, and they are tasty.
2. f=["apple," "pear," "banana," "orange"];
The ideal output is: The fruits are apple,pear,banana,orange, and they are tasty.
3. f=["apple,"];
The ideal output is: The fruits are apple, and they are tasty.
** There is no space between the comma and the next string.
However, the the program gave me :
The fruits are apple, and they are tasty.
The fruits are pear, and they are tasty.
The fruits are banana, and they are tasty.
for the first case
The fruits are apple, and they are tasty.
The fruits are pear, and they are tasty.
The fruits are banana, and they are tasty.
The fruits are orange, and they are tasty.
for the second case
The third case seems working.
Please help.

채택된 답변

Adam Danz
Adam Danz 2019년 4월 29일
strjoin()
f=["apple," "pear," "banana," "orange"];
A=sprintf('The fruits are %s and they are tasty.\n',strjoin(f));
  댓글 수: 4
Adam Danz
Adam Danz 2019년 4월 29일
Yep, check out the second input to strjoin();
help strjoin
Here's your text without spaces
A=sprintf('The fruits are %s and they are tasty.\n',strjoin(f,''));
Angus Wong
Angus Wong 2019년 4월 29일
Thank you very much again!

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by