How can I use COM objects with a "parfor" loop via "actxserver"?

1 view (last 30 days)
I am creating a MATLAB script where I invoke commands on a COM server created with "actxcommand". For example,
MotorCAD_File = 'C:\mymcadfile';
parfor (i = 1:10)
mcad = actxserver('motorcad.appautomation')
invoke(mcad, 'LoadFromFile', [MotorCAD_File '.mot']);
invoke(mcad, 'Quit');
mcad = 0;
end
When I run this code with more than one parallel worker, I get the following error:
Error using MotorCAD_DiaANDMagnet_Mot_Creation
Error: The remote procedure call failed
How can I avoid this error?

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 26 May 2023
When using "actxserver" with a "parfor" loop, it should be defined as a "parallel.pool.Constant", as follows:
a = parallel.pool.Constant(@() actxserver('motorcad.appautomation'));
parfor (i = 1:10)
mcad = a.Value;
invoke(mcad, 'LoadFromFile', [MotorCAD_File '.mot']);
invoke(mcad, 'Quit');
mcad = 0;
end
For more information on "parallel.pool.Constant", please see the following documentation link:
Using the code above, this error should not occur.

More Answers (0)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!