run two scripts with timer

Hello, I have two scripts that I want to execute in parallel. I can't send each script to different threads, because workers don't support plots nor send info between them.
I think we can update the plot from a worker using queued data, but I have no idea how. Also, timer is supposed to do this, I set it up like this:
%main script
f=figure('units','Normalized','Outerposition',[0 0 1 1]);
grid('on');
axis('auto')
h=animatedline('color','b','marker','.');
%% variables
i=1;
i_max=600;
x=[];
means=[];
count=0;
count_max=100;
espera=count_max/20;
i_max=600;
running=true;
t = timer('TimerFcn', 'script1;','StartDelay',1,'ExecutionMode','singleShot');
tt = timer('TimerFcn', 'script2;','StartDelay',1,'ExecutionMode','singleShot');
start(t);
start(tt);
disp('done')
However, this only runs t first and then tt. The output is:
>> main
done
start script1
end script1
start script2
end script2'
The scripts are:
%script1
disp('start script1')
while i<=i_max
x=[x;sin(i)+rand*randi([1 5])];
for k=1:lh
addpoints(h_notsave(k),i,x(i));
end
drawnow
i=i+1;
count=count+1;
end
running=false;
disp('')
disp('end script1')
%script2
disp('start script2')
while running
if count>=count_max
data=x(end-count_max+1:end);
means=[means;mean(data)];
count=0;
disp('inside if')
end
pause(espera);
end
disp('end script2')

5 Comments

I don't get it. If you want to run these two scripts at the same time, why don't you include the code of the 2nd one into the loop of the first one? Why is it needed to distribute the work over two scripts? This looks like an overly complicated approach.
It would be a horror, if you can run two scripts simultaneously. Remember that scripts share their workspace with the caller. I definitely recommend not to try any parallel tricks with scripts, but only with functions and with defining directly and explicitly, which variables are shared.
Hi, the first script it will read from an input (arduino), and if i add second script within the wile loop of the first script, sample rate drops a lot.
@kira: Of course the 2nd script slows down the processing due to the pause and disp commands. The iterative growing of the array is extremely inefficient also. Use a pre-allocation. Running scripts in parallel is not a magic tool to let everything run faster.
kira
kira on 11 Jan 2019
Edited: kira on 11 Jan 2019
disp and pause are just for debugging now
if i run second script within first script, i get a sample rate of around 25/s
if i run just the first scritp i get a sample rate of arounf 100/s
that's why i want to run second script independtly of the first...
@kira: I cannot read your screen or your mind. Currently I see the code, which contains commands for debugging. I cannot know, how the real code looks like. Please post it.
As far as I understand, the actual problem is not to run 2 scripts in parallel, but to accelerate some code to get a sample rate of 100/s again. I'm convinced, that this is easier to solve than with mutli-threading. E.g. you do not need an screen update with a frequency of 100Hz, because you cannot see it at all on a screen running at 60Hz.
Maybe a multithreading is useful, but in a completely different way.

Sign in to comment.

Answers (0)

Categories

Tags

Asked:

on 10 Jan 2019

Commented:

Jan
on 11 Jan 2019

Community Treasure Hunt

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

Start Hunting!