Info

この質問は閉じられています。 編集または回答するには再度開いてください。

Need help writing an if statement to change a plots name based on the data plotted

1 回表示 (過去 30 日間)
Michael Clopton
Michael Clopton 2020 年 5 月 21 日
閉鎖済み: MATLAB Answer Bot 2021 年 8 月 20 日
The code I have so far is as follows it creates a graph that is a polygon and sides are determined based on user input.
function quiz31(~)
sides = input('input the number of sides you want;, ');
if sides <= 0
sides = sides *-1;
disp('only positive numbers allowed')
end
if sides == 5
title('Pentagon')
elseif sides == 3
title('Triangle')
elseif sides == 6
title('Hexagon')
else side == 4;
title('square')
end
degrees=2*pi/sides;
theta=0:degrees:2*pi;
radius=ones(1,numel(theta));
polarplot(theta,radius)
end
  2 件のコメント
dpb
dpb 2020 年 5 月 21 日
  1. Your first clause/error test will still pass thru a zero number of sides
  2. So you set a title; what you're missing is that you need to save the name to a variable in the logic and then add the title after you plot the graph...

回答 (1 件)

Monalisa Pal
Monalisa Pal 2020 年 6 月 22 日
Are you looking for something like this?
function quiz31(~)
sides = input('input the number of sides you want;, ');
if sides <= 0
%sides = sides *-1;
%disp('only positive numbers allowed')
error('only positive numbers allowed');
elseif (sides ~= 5) && (sides ~= 3) && (sides ~= 6) && (sides ~=4)
error('only 2, 3, 5 or 6 sides are allowed');
end
if sides == 5
%title('Pentagon')
str = 'Pentagon';
elseif sides == 3
%title('Triangle')
str = 'Triangle';
elseif sides == 6
%title('Hexagon')
str = 'Hexagon';
elseif sides == 4
%title('square')
str = 'square';
end
degrees=2*pi/sides;
theta=0:degrees:2*pi;
radius=ones(1,numel(theta));
polarplot(theta,radius);
title(str);
end

この質問は閉じられています。

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by