I am trying to run this matlab code:
**************************************
function data=dbmoon(N,d,r,w)
clear all; close all;
if nargin<4, w=6;
elseif nargin<3, r=10;
elseif nargin<2, d=1;
elseif nargin<1, N=1000;
end
N1=10*N;
done=0; data=[]; tmp1=[];
while ~done,
tmp=[2*(r+w2)*(rand(N1,1)-0.5) (r+w2)*rand(N1,1)];
tmp(:,3)=sqrt(tmp(:,1).*tmp(:,1)+tmp(:,2).*tmp(:,2));
idx=find([tmp(:,3)>r-w2] & [tmp(:,3)<r+w2]);
tmp1=[tmp1;tmp(idx,1:2)];
if length(idx)>= N,
done=1;
end
end
data=[tmp1(1:N,:) zeros(N,1);
[tmp1(1:N,1)+r -tmp1(1:N,2)-d ones(N,1)]];
save dbmoon N r w d data;
**************************************
When i run this code i see the this error code:
***************************************
??? Reference to a cleared variable N.
Error in ==> dbmoon at 8
N1=10*N;
****************************************
How do i correct this error? If you can please help me.
Take good care of you. Bye.
> function data=dbmoon(N,d,r,w)
> clear all; close all;
> N1=10*N;
Of course "clear all" clears all variables. And afterwards you cannot access the values of the clear variables.
A perfect example to feed this thread:
http://www.mathworks.com/matlabcentral/newsreader/view_thread/292731
Jan
How can i correct this problem. I have seen links who you send it. But i cant.
> How can i correct this problem.
Simply remove the "clear all; close all;" line.
It clears the variables you need for the computations! So just don't do this.
"close all" closes all open figures - are you sure this is what you want?
Did anybody suggest to insert some "clear all" commands? The ask him to visit this thread and to stop confusing beginners.
Try in the command line:
v = 91;
clear all
disp(v)
This fails, of course.
I'd suggest reading
help clear
also.
Kind regards and welcome to Matlab, Jan
Remove this line from your function
clear all; close all;
Ross
when i delete that line i see "??? Input argument 'N' is undefined." error.
how can i correct this file :(
how are you calling the function?
like this:
dbmoon(1000,1,10,6)
from the look of the code, there are 2 problems:
1. the variable w2 is never defined
2. the code
if nargin<4, w=6;
elseif nargin<3, r=10;
elseif nargin<2, d=1;
elseif nargin<1, N=1000;
end
doesn't do the intended job.
Perhaps it should be more like this:
if nargin<4, w=6;end
if nargin<3, r=10;end
if nargin<2, d=1;end
if nargin<1, N=1000;end
Ross