if i have a counter how would i specify that every time the counter goes up in increments of 1 to execute a command ?

7 views (last 30 days)
specifically how to make this for loop take the cars in queue(or at the red light)and have them go
clc %This code made me lose alot of sleep
clear
n=4500;%this is just a number that always produces more then enough random numbers
x=zeros(n,1);%here i am initializing vectors
y=zeros(n,1);
z=zeros(n,1);
rlight=25;%optimum red light length
glight=60-rlight;
greencars= zeros(n,1);%greencars are cars that go through the light
inqueue=zeros(n,1);%inqueue are cars waiting for light to change
counter=zeros(121,1);%initializing a counter for the purpose of cycles
for time=1:7200
if (mod(time,60)==0)
counter=counter+1;
end
end
for i=1:n;
if (i == 1)
l=(.5+4.5*rand);
x(i,1)=l;
y(i,1)=mod(x(i),60);
else
l=(.5+4.5*rand);
x(i,1)=x(i-1,1)+l;
y(i,1)=mod(x(i),60);
end
if y(i,1)<=35
greencars(i,1)=1
else y(i,1)>35
inqueue(i,1)=1
end
if x(i, 1) > 7200
break; % break out of loop
end
queue=sum(inqueue);
wait=inqueue;
line=wait-1;
sec_cross=3.2+line*1.4;
end
sum(y)
sum(greencars)
sum(inqueue)

Answers (1)

Walter Roberson
Walter Roberson on 1 Dec 2012
There is no way to execute a command automatically as a result of changing a normal numeric variable -- not unless there is something that can be done with addlistener (and I would not expect there to be.)
If you need to execute a command automatically as a result of changing a variable by a certain value, you would probably need to define your own Object Oriented Programming (OOP) class.
It would probably be easier, though, to do all the changes to the variable by calling a function (that you would define) that would do all of the checking for sufficient changes and would call the routine when the conditions were right.
It would probably be better to reconsider how your code should operate.

Tags

Community Treasure Hunt

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

Start Hunting!