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

1 visualizzazione (ultimi 30 giorni)
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?

Risposta accettata

MathWorks Support Team
MathWorks Support Team il 26 Mag 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.

Più risposte (0)

Categorie

Scopri di più su Use COM Objects in MATLAB in Help Center e File Exchange

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by