HELP: received error when trying to run two functions in parallel.

1 view (last 30 days)
I am trying to run two functions in parallel so that they will plot a real-time graph at the same time. My code looks like:
tf = 60;
parfor i = 1:2
if i == 1
disp('OpenBCI start');
[EEGfilt, EMGfiltNormal, t] = OpenBCIfunction(tf);
else
disp('Flex Sensor start');
[R_f0, R_f1, R_f2, R_f4, R_f5, t] = Glovefunction(tf);
end
end
When I run the code, I get the command window to display "OpenBCI start" and "Flex Sensor start". However, I never get a graph to show up and right after "Flex Sensor start" I recieve this message in the command window.
Caught "std::exception" Exception message is:
Unknown exception
Error using OpenBCI_Glove_Parallel (line 9)
Unknown exception
How do I fix this so I can get these two functions to run in parallel and output individual live graphs? (The graphs are being plotted within the functions.)

Answers (1)

Raymond Norris
Raymond Norris on 9 Apr 2021
Edited: Raymond Norris on 9 Apr 2021
It's unclear to me what could be causing the exception. Might need to troubleshoot this with Technical Support (support@mathworks.com).
If the functions OpenBCIfunction and Glovefunction generate the graphs, then they're going to generate them via the headless workers -- i.e. no graphs. However, if the initial graphs are created a prior, then the functions can send data back to the main MATLAB, which can intern update the graphs. Take a look at parallel.pool.DataQueue for an example that might work for you.
  2 Comments
Madison Bates
Madison Bates on 9 Apr 2021
I see a lot of examples for updating a surface plot with a parfor loop. However, I am trying to plot multiple signals in 2 different 2D plots. Is there an easy way to do this? At the beginning of my code, I wrote this:
parpool('local',2); %creates and returns a pool with a specified number of workers
q = parallel.pool.DataQueue;
afterEach(q,@(data) updatePlot(surface,data));
I also created the two figures before the parfor loop. My parfor loop looks like this now:
parfor ii = 1:2
if ii == 1
disp('Flex Sensor start');
[R_f0, R_f1, R_f2, R_f4, R_f5, t] = Glovefunction(tf);
else
disp('OpenBCI start');
[EEGfilt, EMGfiltNormal, t] = OpenBCIfunction(tf);
end
send(q, ii);
end
Thank you.
Raymond Norris
Raymond Norris on 9 Apr 2021
I would suggest two data queues. Here's a sketch of what you could do.
parpool('local',2);
glove_h = figure();
glove_q = parallel.pool.DataQueue;
afterEach(glove_q,@(data)updateGlovePlot(glove_h,data));
bci_h = figure();
bci_q = parallel.pool.DataQueue;
afterEach(bci_q,@(data)updateBciPlot(bci_h,data);
parfor ii = 1:2
if ii == 1
disp('Flex Sensor start');
[R_f0, R_f1, R_f2, R_f4, R_f5, t] = Glovefunction(tf);
send(glove_q,[ data(s) to be passed to update the plot ])
else
disp('OpenBCI start');
[EEGfilt, EMGfiltNormal, t] = OpenBCIfunction(tf);
send(bci_q,[ data(s) to be passed to update the plot ]);
end
end
The "data(s) to be passed to update the plot" is probably based on the variables passed back form calling the Glovefunction / OpenBCIfunction.

Sign in to comment.

Categories

Find more on Parallel for-Loops (parfor) 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!