I am trying to display the sentence:
The approximate work done is {W} kJ.
(Where W is the calculated work variable) How can I do this using just one line? I'm fairly certain I need to use the num2str command. Thanks in advance!
Right now this is what I have, but I want it to be all one line...
disp('The approximate work done is: ')
disp(W)
disp('kJ')

답변 (1개)

the cyclist
the cyclist 2014년 2월 10일
편집: the cyclist 2014년 2월 10일

2 개 추천

disp(['The approximate work done is: ',num2str(W),' kJ'])
The way I personally approach constructing things like this is to first write out the sentence without using the variable:
disp(['The approximate work done is: 123 kJ'])
then isolate the numeric piece, using concatenation:
disp(['The approximate work done is: ','123',' kJ'])
and then substitute in the num2str as above.

댓글 수: 3

Kelsey
Kelsey 2014년 2월 10일
Thank you so so so much! :-)
the cyclist
the cyclist 2014년 2월 10일
The best form of thank you is accepting the answer. :-)
Thomas Seers
Thomas Seers 2014년 2월 10일
Accept the answer Kelsey!

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

카테고리

도움말 센터File Exchange에서 Language Support에 대해 자세히 알아보기

태그

질문:

2014년 2월 10일

댓글:

2014년 2월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by