How can I use COM objects with a "parfor" loop via "actxserver"?
1 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2023-5-26
回答: MathWorks Support Team
2023-6-5
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?
采纳的回答
MathWorks Support Team
2023-5-26
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Use COM Objects in MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!