MATLAB Answers

kira
0

run two scripts with timer

Asked by kira
on 10 Jan 2019
Latest activity Commented on by Jan
on 11 Jan 2019
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

Jan
on 11 Jan 2019
@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
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...
Jan
on 11 Jan 2019
@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.

Tags

0 Answers