Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

??? Reference to a cleared variable

202 views
Skip to first unread message

Umit Yilmaz

unread,
Oct 15, 2010, 5:48:04 PM10/15/10
to
Hi friends,

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.

Jan Simon

unread,
Oct 15, 2010, 6:38:04 PM10/15/10
to
Dear Umit,

> 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

Umit Yilmaz

unread,
Oct 15, 2010, 7:02:04 PM10/15/10
to
Thanks Mr. Simon;

How can i correct this problem. I have seen links who you send it. But i cant.

Jan Simon

unread,
Oct 15, 2010, 8:01:03 PM10/15/10
to
Dear Umit Yilmaz,

> 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

Ross W

unread,
Oct 15, 2010, 8:04:05 PM10/15/10
to
"Umit Yilmaz" <umity...@hotmail.com> wrote in message <i9amhc$dev$1...@fred.mathworks.com>...

> Thanks Mr. Simon;
>
> How can i correct this problem. I have seen links who you send it. But i cant.

Remove this line from your function

clear all; close all;

Ross

Umit Yilmaz

unread,
Oct 16, 2010, 4:26:06 AM10/16/10
to
Thanks for your helps but i cant solve to problem when delete "clear all; close all;" line.

when i delete that line i see "??? Input argument 'N' is undefined." error.

how can i correct this file :(

Ross W

unread,
Oct 16, 2010, 6:25:09 AM10/16/10
to
"Umit Yilmaz" <umity...@hotmail.com> wrote in message <i9bniu$7gf$1...@fred.mathworks.com>...

> Thanks for your helps but i cant solve to problem when delete "clear all; close all;" line.
>
> 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

Umit Yilmaz

unread,
Oct 16, 2010, 5:20:03 PM10/16/10
to
yes it is beginning to work :) thank you so much :)
0 new messages