How do you create a a display function that includes changing variables?

7 ビュー (過去 30 日間)
Justin Tsui
Justin Tsui 2015 年 2 月 14 日
回答済み: Andrew Phung 2015 年 2 月 15 日
Is it correct to use the disp and the num2str function together and what additional formatting is needed to combine them?
For example: I want to add a changing variable into the text "An area of a circle with radius r cm equals an area of A cm^2." Where r and A are changing variables.
  1 件のコメント
Stephen23
Stephen23 2015 年 2 月 15 日
Actually disp displays numeric values too, so it is not required to use num2str before disp. Try it!

サインインしてコメントする。

回答 (1 件)

Andrew Phung
Andrew Phung 2015 年 2 月 15 日
If you want r and A to be able to change in the display when the values are changed in the script, use brackets after the open parenthesis and before the close parenthesis. This indicates to MATLAB that there are strings and variables in your disp command.
For example:
r = 7;
A = pi*r^2;
disp(['An area of a circle with radius ', num2str(r),' cm equals an' ...
' area of ', num2str(A),' cm^2.'])
The output would be
An area of a circle with radius 7 cm equals an area of 153.938 cm^2.

カテゴリ

Help Center および File ExchangeWorkspace Variables and MAT Files についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by