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

### cassie (view profile)

on 2 Mar 2014
Latest activity Edited by cassie

on 19 Apr 2014

### Will (view profile)

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

cassie

### cassie (view profile)

on 2 Mar 2014
wow... aren't we snarky today! I posted it above... it's all the way at the bottom of my code. Have I done something wrong that would make it invisible to you? If so I will happily correct it.
cassie

### cassie (view profile)

on 2 Mar 2014
Or just point me toward this "Walter" character that you speak of...
Image Analyst

### Image Analyst (view profile)

on 2 Mar 2014
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 (view profile)

on 2 Apr 2014

length(xclose) should be length(normalx) and this will work correctly. Too late, I know, because this referenced an assignment that I made :)

vaka sindhu

### vaka sindhu (view profile)

on 2 Apr 2014
sir i am new to this matlab i want to satisfy the equation (512*512*log2(B))/>=520000 by keeping what value of B this equation will be satisfied answer is 16 .. i want how to do code for this equation in matlab by taking B values automatically to satisfy this equation
cassie

### cassie (view profile)

on 19 Apr 2014
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. ### Image Analyst (view profile)

on 2 Mar 2014

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

cassie

### cassie (view profile)

on 2 Mar 2014
xclosed was defined, I think, in the previous subfunction. do I need to "define" them before the for loop somehow? I'm still figuring out exactly how the functions work and it is NOT coming naturally. If it helps, here is a link to the project instructions so you can see the full scope of what I'm trying to accomplish: http://ef.engr.utk.edu/ef230-2014-01/projects/polygon-morph-s2014/ Thanks!
Image Analyst

### Image Analyst (view profile)

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