Isolate Seconds from Matlab Clock - matlab

I am trying to achieve two things:
Start a function if the current time is an exact 10 seconds multiple and
Run the function in a loop every 10 seconds after that
I have a cronjob on AWS pulling data every 10 seconds. I want my MATLAB function to run specifically once that has been updated (every 10 seconds).
The function takes less than 10 seconds to run. Currently, I am doing:
tic
function
toc
elapsedTime=toc;
pause(10-elapsedTime);
but this dosent seem to be working. On top of that, I want the entire function to start at 1:00:10 (seconds).
I think using Matlab's
format shortg
c = clock
fix(c)
and some how starting at sec = [0,10,20....,60] would work, but I am not sure how to isolate the seconds from the system clock.

Related

Is there a way to do a millisecond level delay in matlab?

Help for 'PAUSE' says
PAUSE(n) pauses for n seconds before continuing, where n can also be a
fraction. The resolution of the clock is platform specific. Fractional
pauses of 0.01 seconds should be supported on most platforms.
But in my case pause(0.01) doesn't do anything at all (pause, pause(n) with whole number n works)
Is there any way to make a millisecond level delay (50 ms, 100 ~ 500 ms) delay in matlab?
Matlab version is
MATLAB Version 7.9.0.529 (R2009b)
64 bit on a Windows 10 64 bit Home edition
I see two options. Let's call them the looping option and the native option. The first is just using a while loop to check if your desired time to wait is already reached. You can do this with MATLAB's stopwatch timer tic and toc. This is the normal time (not the CPU-time). As you are writing a loop, which runs at maximum speed, you might encounter a high CPU-usage but this should be OK if it is only for a couple of milliseconds.
%% looping
% desired time to wait
dt_des = 0.001; % 1 ms
% initialize clock
t_st = tic;
% looping
while toc(t_st) < dt_des
end
% read clock
toc(t_st)
The native option is using pause (make sure that you have enabled it once with pause('on')). I assumed from your question that this does not always work -- however, it does on my system (R2019b, windows 10).
%% use pause()
tic
pause(0.001)
toc
The results of both are
Elapsed time is 0.001055 seconds.
Elapsed time is 0.001197 seconds.
It's not too accurate but you might get better results if you tune the numbers on your PC.
you can also use
java.lang.Thread.sleep(10);
if you are using an old matlab version, see discussion here.

Google OR-Tools: Minimize Total Time

I am working on a VRPTW and want to minimize the total time (travel time + waiting time) cumulated for all vehicles. So if we have 2 vehicles one that starts at time 0 and returns at time 50 and one that starts at time 25 and returns at time 100, then the objective value would be 50+75=125.
Currently I have implemented the following code:
for i in range(data['num_vehicles']):
routing.AddVariableMinimizedByFinalizer(
time_dimension.CumulVar(routing.End(i)))
However, this seems like it is only minimizing the time we arrive back at the depot.
Also it results in very high waiting times.
How do I implement it correctly in Google OR tools?
This is called the span.
See the SetSpanCostCoefficientForVehicle method for one vehicle.
You can also set it for all vehicles.

How to stop fmincon after running an elapsed time of T sec?

I am running fmincon for a gird. For some points in the grid there is no solution, exit flag=-2, and it takes half an hour sometimes to return the flag and start the next point in the grid. However, for the other points the answer is revealed in less than a min.
So I thought one possible solution to reduce the unnecessary running time is to define a stop criteria based on elapsed time for each point in the grid. In other words, How to stop fmincon after running an elapsed time of T sec?
You can use an output function:
tic
fmincon(.....,optimset('OutputFcn',#outfun,'MaxFunEval',20000))
outfun.m :
function stop = outfun(x,optimValues,state)
stop = toc>T;

VMD terminates simulation before completion

I am trying to run a 1 ns simulation using VMD/NAMD on top of my 200 ps simulation, so I set the program to run 800000 with a timestep of 1. However, the next day (it took about 12 hours) it was complete, but I only had ~16500 frames. Anyone know why the program only collected so many frames? I have a similar issue with running different simulations: the amount I ask it to run and the number of frames I get are not the same.

Taking continuous inputs in MATLAB

I want to take data from bluetooth into Matlab every 10 to 15 seconds. I figured out a way to transfer data to Matlab via bluetooth. The problem I am facing is that I want Matlab to execute a set of commands after a time interval to take input from bluetooth. How can I do this?
You could create a TIMER OBJECT, that you set to execute a function that collects and processes data every 10 to 15 seconds.