필터 지우기
필터 지우기

Multiple lines in a string

조회 수: 5 (최근 30일)
Zach
Zach 2011년 9월 11일
I am trying to obtain multiple lines in a string. The first part is the following:
name = 'Alice'; age = 12; str = fprintf('%s will be %d this year.\n', name, age);
Alice will be 12 this year.
I want the end result to obtain the this:
Alice will be 12 this year.
John will be 13 this year.
Granted I could just repeat the intial command but that could take long with multiple entries... Im looking for a command that can fit one line. Thanks

채택된 답변

Fangjun Jiang
Fangjun Jiang 2011년 9월 11일
Data={'Alice','John','Joe','Jill';12 13 14 15};
fprintf('\n%s will be %d this year.\n',Data{:});

추가 답변 (1개)

Image Analyst
Image Analyst 2011년 10월 1일
Your problem was that you used fprintf() instead of sprintf() so what got stored in str was not what you thought it should be. fprintf() returns the number of bytes written out, not the string, while sprintf() returns the "str" string that you said you want.

카테고리

Help CenterFile Exchange에서 Numeric Types에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by