Output argument "min_dist" (and maybe others) not assigned during call to
3 views (last 30 days)
Show older comments
this is my main code I am trying to calculate the minimum distance between any number of points. In the case of a line there a re multiple, so standard deviation is used to find the ideal point.
clc;clear all;x=1;
data_points = input('How m
if true
% code
endany data points would you like to enter: ');
integer_test =~ mod(data_points,1);
while x == 1;
if data_points < 0 || integer_test == 0;
data_points = input('Invalid! Enter a positive integer: ');
integer_test =~ mod(data_points,1);
else data_points > 0 && integer_test == 1;x=2;
x_range = input('Enter the randge and spacing of x [xmin:dx:xmax]:');
y_range = input('Enter the randge and spacing of y [ymin:dy:ymax]:');
points = 1;xmin=min(x_range);xmax=max(x_range);ymin=min(y_range);ymax=max(y_range);
prompt = ['Enter point ' num2str(points) ' [x,y] (' num2str(xmin) '<=x<=' num2str(xmax) ',' num2str(ymin) '<=y<=' num2str(ymax) '):'];
diet(points,:) = input(prompt);
while points <= data_points
if diet(points,2)>ymax || diet(points,2)<ymin || diet(points,1)<xmin || diet(points,1)>xmax
prompt_2 = ['Please enter point ' num2str(points) ' again [x,y]: '];
fprintf('The entered point is not within the range of %g<=x<=%g and/or %g<=y<=%g\n',xmin,xmax,ymin,ymax);7
diet(points,:) = input(prompt_2);
else diet(points,2)<ymax & diet(points,2)>ymin & diet(points,1)>xmin & diet(points,1)<xmax;
sharpie(points,:) = [points,diet(points,1),diet(points,2)];
points = points +1;
if points <= data_points
prompt = ['Enter point ' num2str(points) ' [x,y] ...
(' num2str(xmin) '<=x<=' num2str(xmax) ',' num2str(ymin) '<=y<=' num2str(ymax) '):'];
diet(points,:) = input(prompt);
end
end
end
[min_dist,min_point,final_array] = ... final_function(x_range,y_range,sharpie);
complete_array = [sharpie(:,1:3) (final_array)'];
fprintf('The point that minimizes distnace is(a,b) = (%g,%g)\n\n',min_point(1),min_point(2))
fprintf('The total minimum distance is %g\n\n',min_dist)
disp(complete_array)
x=complete_array(:,2);x_2 = min_point(1);z=complete_array(:,1);
y=complete_array(:,3);y_2 = min_point(2);
plot(x,y,'bo','MarkerFaceColor','b','MarkerSize',10)
hold on;
plot(x_2,y_2,'ro','MarkerFaceColor','r','MarkerSize',10)
text(x-.09,y,num2cell(z),'Color','w');
xlabel('x-coordinate','FontSize',14)
ylabel('y-coordinate','FontSize',14)
title('Final Project by Nate Steele','FontSize',14)
h = legend('Entered Points','Minimum Point');
set(h,'FontSize',14,'Location','best')
end
end
when i run this function I get the error in the title
function [min_dist,min_point,final_array] = final_function(x_range,y_range,sharpie)
[sharpie_size,k] = size(sharpie);
distance = zeros(1,sharpie_size);
final_array = sum(distance) + 1000000;
for i = x_range
for j = y_range
for times = 1:sharpie_size
distance(times) = sqrt((i-sharpie(times,2))^2+(j-sharpie(times,3))^2);
end
if std2(distance) < std2(final_array)
min_point = [i,j];
final_array = distance;
min_dist = sum(final_array);
end
distance = zeros(1,sharpie_size);
end
end
this is the information i am supposed to calculate
How many data points would you like to enter: 4
Enter the randge and spacing of x [xmin:dx:xmax]:[0:.1:10]
Enter the randge and spacing of y [ymin:dy:ymax]:[0:.1:10]
Enter point 1 [x,y] (0<=x<=10,0<=y<=10):[1.2,2.8]
Enter point 2 [x,y] (0<=x<=10,0<=y<=10):[3,7]
Enter point 3 [x,y] (0<=x<=10,0<=y<=10):[5.67,1.02]
Enter point 4 [x,y] (0<=x<=10,0<=y<=10):[7.8,8.2]
0 Comments
Answers (1)
Wayne King
on 14 Dec 2013
Edited: Wayne King
on 14 Dec 2013
Without running your code, I see that you have min_dist assigned inside an if statement
if std2(distance) < std2(final_array)
min_point = [i,j];
final_array = distance;
min_dist = sum(final_array);
end
What if std2(distance) > std2(final_array) ?
Then min_dist is not assigned any value.
Is that really want you want to do? It's not good practice to have a function outputting an argument which is possibly not even created in the function.
Can you at least assign it some fallback value in case that if statement evaluates to false?
2 Comments
Image Analyst
on 14 Dec 2013
This will help with that problem immensely: http://blogs.mathworks.com/videos/2012/07/03/debugging-in-matlab/
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!