How can I run 2 tasks in parallel?
조회 수: 2 (최근 30일)
이전 댓글 표시
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
Walter Roberson
2018년 7월 2일
You should consider using a circular buffer; they are faster than what you are doing now, which involves a lot of memory re-allocations.
채택된 답변
OCDER
2018년 7월 2일
편집: OCDER
2018년 7월 2일
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
OCDER
2018년 7월 2일
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)); %<<<<<<<<<<<<<<<<<<<<<<<
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 National Instruments Frame Grabbers에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!