i want to use the parameters inside the loop in function circleintersection after the loop and the error was (referance to cleared variable)wt can i do???

조회 수: 2 (최근 30일)
if d(1,m)<R
circlequ(netXloc(j),netYloc(j),50,1000);
x(1,n)=netXloc(j)
y(1,n)=netYloc(j)
r=50.*ones(1,length(x));
n=n+1;
end
[a,b]=circleintersection(x,y,r);

답변 (1개)

David Young
David Young 2011년 3월 23일
Variables are not automatically cleared at the end of a loop. There is some other problem, which cannot be diagnosed from the code fragment you have posted. Maybe you clear d, R, m, or j before the loop begins? Use breakpoints, or just print out some values, in order to find out what variable has not got a value.
  댓글 수: 1
SAM alimostafa
SAM alimostafa 2011년 3월 23일
i print out the values of x,y and r on the workspace and it has a result this is the whole code:
clc;
clear all;
close all
noOfNodes = 100;
rand('state', 0);
figure(1);
clf;
hold on;
L = 1000;
R = 100; % maximum range;
netXloc = rand(1,noOfNodes)*L;
netYloc = rand(1,noOfNodes)*L;
dis=zeros(noOfNodes,1);
A=[netXloc' netYloc'];
for i = 1:noOfNodes
plot(netXloc(i), netYloc(i), '.');
text(netXloc(i), netYloc(i), num2str(i));
end
for t=300:100:300
x1=t;% we can change this function so the target path will be change
y1=t;
m=1;
n=1;
x=[];
y=[];
r=[];
for j=1:noOfNodes
d(1,m)=sqrt((x1-netXloc(j))^2+(y1-netYloc(j))^2);
if d(1,m)<R
circlequ(netXloc(j),netYloc(j),50,1000); %draw circle around the sensed sensors
x(1,n)=netXloc(j)
y(1,n)=netYloc(j)
r=50.*ones(1,length(x));
n=n+1;
end
[a,b]=circleintersection(x,y,r);
dis(j)=d(1,m); %distance from target and each node
m=m+1;
end
end
plot(a,b)
hold on
grid

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by