Changing a vector element for a certain time and then change it back again

9 views (last 30 days)
The behaviour at a car park / charging station
is as follows: A car arrives, looks for the next parking space and leaves after a certain time. Now I would like to simulate this behaviour.
How can I change the numerical value of a vector element (as a parking space) for a certain time so that it automatically changes back again afterwards?
iniTime = clock;
limit = 24 * 60; %one day in minutes
while etime(clock, iniTime) < limit
charging_station = [0,0,0,1,0,1,0];
stop = length(charging_station);
for i = 1: stop
if charging_station(i) == 0
charging_station(i) = 1;
end
end
end

Answers (1)

Poorna
Poorna on 13 Sep 2023
Hi Patrick,
I understand that you want to change the values of a vector for a certain time to simulate a car park/charging station.
Although, there are no direct ways to achieve that behaviour, you could use “timer” object to achieve the desired behaviour.
You could create a single shot timer and then set the ‘TimerFcn’ callback to a function that reset that parking slot back to 0.
Essentially, after assigning a parking slot (i.e., setting a particular slot to 1), you can create timer object, set the timer delay to the time that you want the slot to be occupied, set the “TimerFcncallback, and then start the timer as shown below:
%create a timer object
t = timer;
t.StartDelay = charging_time; %charging_time is the amount of time after which the car leaves
%my_callback is the function that resets the slot, and i is the index of the parking slot.
t.TimerFcn = {@my_callback,i};
start(t);
Here is an example code for how “my_callback” function could look like (feel free to modify it according to your requirements):
function my_callback(obj,event,index)
charging_station(index) = 0;
fprintf("Charging station at %d is free\n", index);
end
Consider making the variable "charging_station a global variable.
Kindly refer to the following links to know more about the functions used above:

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!