How can I use COM objects with a "parfor" loop via "actxserver"?
1 view (last 30 days)
Show older comments
MathWorks Support Team
on 26 May 2023
Answered: MathWorks Support Team
on 5 Jun 2023
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
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.
0 Comments
More Answers (0)
See Also
Categories
Find more on Use COM Objects in MATLAB 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!