# 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. "

조회 수: 3(최근 30일)
cassie 2014년 3월 2일
편집: cassie 2014년 4월 19일
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표시숨기기 이전 댓글 수: 5
Image Analyst 2014년 3월 2일
편집: Image Analyst 2014년 3월 2일
I didn't see it at first when I told Firefox to highlight all occurrences of the word, but I see it now. Maybe because it's not indented like usual I didn't notice it. You can type control-a and then control-i in MATLAB to "fix up" or standardize the indenting. Walter monitors this forum pretty actively so I'm sure he'll see your question, if he hasn't already, but he doesn't answer every question. Look who answers the questions here and you'll see Walter's name all over the place as well as here http://www.mathworks.com/matlabcentral/answers/contributors

댓글을 달려면 로그인하십시오.

### 채택된 답변

Will 2014년 4월 2일
length(xclose) should be length(normalx) and this will work correctly. Too late, I know, because this referenced an assignment that I made :)
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
cassie 2014년 4월 19일
편집: cassie 2014년 4월 19일
Yes a little late Professor Schle... I mean "Will" :) Feel free to chime in on my most recent question though.. I want to use an image (a car) as a slider button for our last project.

댓글을 달려면 로그인하십시오.

### 추가 답변(1개)

Image Analyst 2014년 3월 2일
Evidently it's not getting into your for loop and defining midx and midy. So what is the value of xclose? It seems your functions are not nested so actually I'm not even sure how it got past the length(xclose) without a "variable not defined error".
##### 댓글 수: 2표시숨기기 이전 댓글 수: 1
Image Analyst 2014년 3월 2일
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()

댓글을 달려면 로그인하십시오.

### 범주

Find more on Variables in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by