more than one input in sprintf

조회 수: 24 (최근 30일)
Liber-T
Liber-T 2011년 7월 19일
Hye, I would like to put two argument in sprintf.
I tried:
sprintf('mu=%s ,nu/omega=%s',mu1,q)
but it doesn't work, only the first one is assigned.
  댓글 수: 4
Liber-T
Liber-T 2011년 7월 19일
double, for both.
Sean de Wolski
Sean de Wolski 2011년 7월 19일
that is your issue - they're doubles not strings. So you should be using %f or %g in your sprintf statement instead of %s which indicates class char. See both Walter's and my suggestions below.

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

채택된 답변

Sean de Wolski
Sean de Wolski 2011년 7월 19일
Works for me...
mu1 = 'hello world'
q = '78'
sprintf('mu=%s ,nu/omega=%s',mu1,q)
Remember you're calling it as a string so mu1 and q both have to be strings
Use %f or %g etc. for numbers that are not strings or convert the numbers to string using num2str
doc sprintf

추가 답변 (1개)

Walter Roberson
Walter Roberson 2011년 7월 19일
That should work fine, provided that mu1 and q are both strings already.
Perhaps you want
sprintf('mu=%g, nu/omega=%g\n', mu1, q)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by