How to change the case to a string so when I run the script the the string instead of the case number is shown?

2 ビュー (過去 30 日間)
m=menu('select shape:', 'triangle','square','pentagon','hexagon','heptagon','octagon')
switch m
case 1
n=3
angle=(n-2)*180
case 2
n=4
angle=(n-2)*180
case 3
n=5
angle=(n-2)*180
case 4
n=6
angle=(n-2)*180
case 5
n=7
angle=(n-2)*180
case 6
n=8
angle=(n-2)*180
end
fprintf('A %-5.2f has %-5.2f sides and %-5.2f degrees\n',m,n,angle)
>> Untitled
m =
1
n =
3
angle =
180
A 1.00 has 3.00 sides and 180.00 degrees

採用された回答

Star Strider
Star Strider 2015 年 4 月 4 日
I believe you want:
fprintf('A %s has %d sides and %-5.2f degrees\n',m,n,angle)

その他の回答 (1 件)

Mahdiyar
Mahdiyar 2015 年 4 月 4 日
編集済み: Mahdiyar 2015 年 4 月 4 日
Hi Frank_m
Use the following code
clc
clear
m=menu('select shape:', 'triangle','square','pentagon','hexagon','heptagon','octagon');
switch m
case 1
m = 'triangle'
s = 1;
n=3
angle=(n-2)*180
case 2
m = 'square'
s = 2;
n=4
angle=(n-2)*180
case 3
m = 'pentagon'
s = 3;
n=5
angle=(n-2)*180
case 4
m = 'hexagon'
s = 4;
n=6
angle=(n-2)*180
case 5
m = 'heptagon'
s = 5;
n=7
angle=(n-2)*180
case 6
m = 'octagon'
s = 6;
n=8
angle=(n-2)*180
end
fprintf('A %-5.2f has %-5.2f sides and %-5.2f degrees\n',s,n,angle)
Regards
  4 件のコメント
Frank_m
Frank_m 2015 年 4 月 4 日
example: "A triangle has 3 sides and 180 degrees"
Mahdiyar
Mahdiyar 2015 年 4 月 4 日
Change the last line to the following line
fprintf('A %s has %-5.2f sides and %-5.2f degrees\n',m,n,angle)%

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

カテゴリ

Help Center および File ExchangePowertrain Blockset についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by