How can I make a function to input a polygon name and have the function plot the polygon?

I am trying to create a code that will be able to plot a polygon by imputing the name. For example: PlotThePolygon(Triangle) . Any help is appreciated!

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 2 月 15 日
function gonname = Triangle
gonname = 'Triangle';
function gonname = Square
gonname = 'Square';
function gonname = Pentagon
gonname = 'Pentagon';
function PlotThePolygon(gonname)
switch(gonname)
case 'Triangle'
... %appropriate plotting here
case 'Square'
... %appropriate plotting here
case 'Pentagon'
... %appropriate plotting here
otherwise
fprintf('I do not know how to plot something named a "%s"\n', gonname);
end
The first functions exist solely for the purpose of allowing the user to omit the quotation marks around what needs to be a string, since PlotThePolygon(Triangle) is asking PlotThePolygon to work upon the result of executing a function named "Triangle" and what it really needs is to be called like PlotThePolygon('Triangle')

カテゴリ

質問済み:

2016 年 2 月 15 日

回答済み:

2016 年 2 月 15 日

Community Treasure Hunt

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

Start Hunting!

Translated by