Asked by cassie
on 2 Mar 2014

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

Answer by Will
on 2 Apr 2014

Accepted Answer

vaka sindhu
on 2 Apr 2014

cassie
on 19 Apr 2014

Sign in to comment.

Answer by Image Analyst
on 2 Mar 2014

cassie
on 2 Mar 2014

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()

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Matt J (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/119857-beginner-matlab-project-function-trouble-instructions-say-create-a-sub-function-that-takes-as-i#comment_199477

## cassie (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/119857-beginner-matlab-project-function-trouble-instructions-say-create-a-sub-function-that-takes-as-i#comment_199479

## Image Analyst (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/119857-beginner-matlab-project-function-trouble-instructions-say-create-a-sub-function-that-takes-as-i#comment_199482

## cassie (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/119857-beginner-matlab-project-function-trouble-instructions-say-create-a-sub-function-that-takes-as-i#comment_199485

## cassie (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/119857-beginner-matlab-project-function-trouble-instructions-say-create-a-sub-function-that-takes-as-i#comment_199488

## Image Analyst (view profile)

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/119857-beginner-matlab-project-function-trouble-instructions-say-create-a-sub-function-that-takes-as-i#comment_199497

Sign in to comment.