How can I make a function to input a polygon name and have the function plot the polygon?
Afficher commentaires plus anciens
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!
Réponses (1)
Walter Roberson
le 15 Fév 2016
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')
Catégories
En savoir plus sur Annotations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!