run two scripts with timer

조회 수: 5 (최근 30일)
kira
kira 2019년 1월 10일
댓글: Jan 2019년 1월 11일
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
kira
kira 2019년 1월 11일
편집: kira 2019년 1월 11일
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
Jan 2019년 1월 11일
@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.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Platform and License에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by