# Beginner Matlab project... function trouble. Instructions say "Create a sub-function that takes as input the x and y values of a closed polygon and calculates x and y values for a new polygon that has vertices at the midpoints of the input polygon. "

1 view (last 30 days)

Show older comments

Here is my code so far, I have a LOT more to do but am stuck on this step. Help is appreciated!

function project_2 %to test subfunctions

clc;

[xinitial,yinitial]=generatepoly(10);%call subfunction from Step2 to generate polygon using given values

[xclose,yclose]=closepoints(xinitial,yinitial); %call subfunction from Step3 to close polygon

plot_function(xclose,yclose,'bo-',4);

%(closepoints(xclose,yclose,'bo-',4))%plot closepoints function?

%[newx,newy]=iterationloop(xclose,yclose); %call subfunction from Step4

[midx,midy]=calcmidpoints(xclose,yclose); %call subfunction from Step5 to generate midpoints

end %end main function

function [xinitial,yinitial]=generatepoly(n) %generates given test values in a matrix

xinitial=[1 2 3 4 3 2 1 3]; %given test points

yinitial=[1 2 1 2 4 3 5 6]; %given test points

end %end Step2

function [xclose,yclose]=closepoints(xinitial,yinitial)

xclose=[xinitial(1:end),xinitial(1)];%connect ending x value to first x value to close poly

yclose=[yinitial(1:end),yinitial(1)];%connect ending y value to first y value to close poly

end %end Step3

function [newx,newy]=plot_function(xclose,yclose,linespec,I)%to plot closed polygon with specified line type a defined number of times

numberofpoints=length(xclose);%define variable to be the number of items in the list xclose

plot(xclose,yclose,linespec); %plot polygon with line type

txt = ['Number of points: ',num2str(numberofpoints)];%define variable txt to include number of points

title(txt)%display title including number of points

end %end Step4, iteration function

function [midx,midy]=calcmidpoints(normalx,normaly)

for n=1:length(xclose); %while n is one less than number of inputs

midx(n)=.5.*(normalx(n)+normalx(n+1));

midy(n)=.5.*(normaly(n)+normaly(n+1));

end %end for loop

end %end Step5

##### 6 Comments

Image Analyst
on 2 Mar 2014

Edited: Image Analyst
on 2 Mar 2014

### Accepted Answer

Will
on 2 Apr 2014

##### 2 Comments

vaka sindhu
on 2 Apr 2014

### More Answers (1)

Image Analyst
on 2 Mar 2014

##### 2 Comments

Image Analyst
on 2 Mar 2014

You can have a whole bunch of functions in a single m-file, as long as the m-file starts with a function and not a script. If the functions are not nested, then each function has only it's own internal variables and whatever was passed in. If a function is nested, contained within another, then it can see what ever variables are in the parent function when the nested/contained function is called.

Put a breakpoint in that function and when it stops there, see what the value of xclose is. Then step line by line and see if it ever gets inside the if. It's always best to define some default values so if something happens it will return something, even if it's just null. For example

function [midx,midy] = calcmidpoints(normalx,normaly)

midx = 0;

midy = 0;

whos xclose

class(xclose)

for n=1:length(xclose); %while n is one less than number of inputs

midx(n)=.5.*(normalx(n)+normalx(n+1));

midy(n)=.5.*(normaly(n)+normaly(n+1));

end % end for loop

end % of calcmidpoints()

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!