How to set maximum calculation time in matlab (without activating Simulink) - matlab

I'm doing simulation in Matlab where some data from the simulation are obtained by executing another software. The idea is when the calculation time is beyond a limit the data from the simulation will not be accepted. How to set a maximum calculation time to automatically stop this unnecessary calculation? I don't use Simulink at the moment.
Thanks in advance!

You can try investigating the use of tic, toc commands if you are doing loop-styled calculations. e.g. at the very start of the calculation:
tic;
just before end of each loop:
if (toc>60) %//However many seconds you want
break;
end
This might not be useful if you don't have loop-style calculations you have direct access to.

Related

Showing progress for vector operation

I am solving a complicate equation that includes a huge matrices size and many operations. Its in vector formula. It takes very long time to finish. So, I need to show which step is done on the screen. When we are using loop we can include counter inside it to show the steps that is done. For example, multiplying two matrices.
clear;
clc;
a=rand(1,5);
b=rand(1,5);
c(1,:)=0;
for i=1:5
c(i)=a(i)*b(i);
fprintf('%d\n ', i);
end
However, if we use vector to multiply two matrices this will be like
c=a.*b
Is there anyway that we can monitor the progress. So, we can show which step is done?
As mentioned here:
There is no built-in functionailty to perform this in MATLAB beyond specifying debug statements and print-to-screen updates at specific sections of the user's code.
Also, something like waitbar is not your solution, as you want to monitor the process of computation which is done by matlab, not yours.

a nonstop Running of my Simulink model

In order to plot the Eb/N0 versus BER performance of my model Simulink and bring out the output (which is a vector of bits) from simulink to workspace, I need to the complete simulation of my model. But, the simulation doesn't stop, it runs hours und hours without stop. Actually, I haven't unterstand this problem as a beginner in Matlab-simulink. But, I am quiet sure that I need to see the end of my work to be able to analyse the performance and then correct if there is a problem.
Thanks in advance
This might be what you want to access data during simulation:
http://www.mathworks.com/help/simulink/ug/accessing-block-data-during-simulation.html

TIC TOC or Profiler

I am writing some codes in which the calculation time is important. I use tic toc function and profiler to measure the time. what is the difference between them?
for a piece of my code, tic toc function states that for example the time is 3 sec but the sum of times in profiler for all lines of my code is not that much!
The profiler is great for finding bottlenecks in your code, and for comparing relative timings for different algorithms, but I wouldn't trust it to give you accurate absolute timings.
For one thing, using the profiler disables many JIT optimisations, so the code may well not run in the same way as it does normally.
Taking timings with tic and toc can be quite subtle, as you need to warm up the code and run it a few times to take an average.
I would recommend you use timeit instead, which handles all of these subtleties for you. In recent versions timeit is part of core MATLAB. If you're using an older version, you can download it from the MATLAB Central File Exchange.
TIC TOC and profiler do very different things.
TIC TOC measures the elapsed time from the call of TIC to the call of TOC. This includes some overhead within the tic functions themselves and for very sorry intervals, is not totally reliable.
Profiler measures the cpu time for the execution of each function within your code. This does not include downtime during which no function is currently executed (the cpu is performing other tasks, including running your matlab code).
There are other things you should do to ensure a precise calculation of time:
Wrap the code in a function to ensure it is JIT compiled.
Warm up the function by running it once prior to profiling.
Run the code multiple times and average the times.
Run the tic/toc function many times, average the result, and subtract that from your total time.
If possible, allocate arrays outside of any loops, rather than appending to an array.

sampling time counter in simulink

how can I take simulink time from "clock" block in simulink and then increase a counter (k)
in a matlab embedded function at every sampling period Ts?
let us say if simulink clock time is t, then we may consider
if mod(t, Ts)==0
k=k+1;
end
but this will not work since simulink time is variable.
Any idea? Thanks.
I'm assuming that you are only interested in doing this for variable step solvers. Let us assume that your sampling time is every two seconds, and your solver is ode23t, with the simulation running for ten seconds. Then, you expect to have the value of your variable be 5.
Now, the way you set up the model is in the screenshot below. Constant is your sampling time, Clock is the source for the time. You need a Rate Transition block to ensure a periodic output for a non-periodic clock input into it. I've set a sampel time of 1 for it.
Finally, in your code, you need a persistent variable. You could also use Simulink's Data Store Read and Write blocks, but this seems simpler to me.

Matlab - Run simulink simulation in loop

Im using a Simulink model in a loop, by changing a parameter.
for i = 1:length(c_slip_sweep);
...
c_slip=c_slip_sweep(i)
tic; [tt2{i},xx2{i},yy2{i}]=sim(model,stop_time); toc
...
end
As the model spend much time to perform (100s), I'm trying to optimize it.
I've only find the "Rapid Accelerator" simulation mode reducing to 70s. But I don't know if there is a better way. Can I compile the model once, and re-use it in the loop ? Or can I give a vector instead a parameter (whithout Parallel Computing Toolbox) ?