How can I run 2 tasks in parallel?
4 views (last 30 days)
Show older comments
I have 2 scripts.
Script1.m___________%infinite while loop which constantly modifying a 'fixed length array'
Script2.m___________% again infinite loop which is processing a live video.
Script1 is independent. But script2 requires the fixed length array generated by script1 before starting image processing on each frame.
I can't merge the scripts because time taken by image processing is significant and will modify script1's independency.
Any alternate non-conventional ideas are also welcome.
Thanks
3 Comments
Walter Roberson
on 2 Jul 2018
You should consider using a circular buffer; they are faster than what you are doing now, which involves a lot of memory re-allocations.
Accepted Answer
OCDER
on 2 Jul 2018
Edited: OCDER
on 2 Jul 2018
You could use two timer objects, one for updating your s vector, and another for extracting the s vector and processing the image with it. See example code and modify the functions script1 and script2.
BUT, this isn't truely in parallel... The real solution require some sort of talk between workers and parallel. See this
%createTimer1.m---------------------------------
function Timer1 = createTimer1()
UserData.s = zeros(1, 100);
UserData.i = 1;
UserData.n = 100;
Timer1 = timer;
Timer1.UserData = UserData;
Timer1.Period = 0.01;
Timer1.TasksToExecute = Inf; %replaces your infinite while loop
Timer1.ExecutionMode = 'fixedSpacing';
Timer1.Timerfcn = @script1;
function script1(Timer1, events)
UserData = get(Timer1, 'UserData');
UserData.s = [UserData.s(2:end) UserData.i];
UserData.i = UserData.i + 1;
set(Timer1, 'UserData', UserData);
%-----------------------------------------------------end of createTimer1.m
%createTimer2.m---------------------------------
function Timer2 = createTimer2(Timer1)
Timer2 = timer;
Timer2.UserData = Timer1;
Timer2.Period = 0.01;
Timer2.TasksToExecute = Inf;
Timer2.ExecutionMode = 'fixedSpacing';
Timer2.Timerfcn = @script2;
function script2(Timer2, events)
UserData = get(get(Timer2, 'UserData'), 'UserData'); %get Timer1's current data
disp(UserData.s); %to show you what's happening
%-----------------------------------------------------end of createTimer2.m
To run these, use the script:
Timer1 = createTimer1();
Timer2 = createTimer2(Timer1);
start(Timer1);
start(Timer2);
9 Comments
OCDER
on 2 Jul 2018
function Timer1 = createTimer1()
UserData.s = zeros(1, 100);
UserData.i = 1;
UserData.n = 100;
UserData.tic = tic; %<<<<<<<<<<<<<<<<<<<<<
Timer1 = timer;
Timer1.UserData = UserData;
Timer1.Period = 0.01;
Timer1.TasksToExecute = 100;
Timer1.ExecutionMode = 'fixedRate';
Timer1.Timerfcn = @script1;
function script1(Timer1, events)
UserData = get(Timer1, 'UserData');
UserData.s = [UserData.s(2:end) UserData.i];
UserData.i = UserData.i + 1;
set(Timer1, 'UserData', UserData);
disp(toc(UserData.tic)); %<<<<<<<<<<<<<<<<<<<<<<<
More Answers (0)
See Also
Categories
Find more on National Instruments Frame Grabbers in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!