Can't pass COM object handle into spmd body

1 次查看(过去 30 天)
I am using an application object opened using ActiveX server,
oApp = actxserver('my.application');
in the definition of an anonymous function,
fHandle = @(x) myFunction(x,oApp);
(I am processing the data 'x' with an external application 'my.application'.)
This function handle works fine when I call it form the MATLAB client, like this:
fHandle(5)
ans =
100
However, when I call it from within spmd body,
spmd
if labindex == 1
y = fHandle(x);
end
.....
end
I am getting an error message like "Trying to reference an un-structured object", and you can see this object is oApp. So, the lab from the spmd pool does not oApp as a valid application object. I tried to open this object inside the spmd body, but I cannot define an anonymous function there (documentation says the same).
Can anybody please give me a hint how to solve this problem? -Thanks a lot!

采纳的回答

matlabUser
matlabUser 2011-10-17
|It looks like there exists a simple solution which involves creating a COM object inside the spmd body, but avoids creating an anonymous function there. One has to create the anonymous function outside the spmd body, and indicate COM object as one of _variables_ (not parameters) like in the example code below. First you define an external function somewhere:|
function y = exFunction(x,oApp,c)
% Find the square of x and does nothing to oApp
y = c * x.^2;
% Display argument oApp to check if it is of type
% COM.Excel_Application
oApp
end
Then the script with spmd body will look like:
matlabpool open 2
% specify the additional parameter:
c = 3; % whatever values you need
%Create a Function handle of function exFunction
fhun = @(x,oApp) exFunction(x,oApp,c);
spmd
if labindex==1
R = rand(2,2)
else
%Create a COM server running Microsoft Excel
oApp = actxserver ('Excel.Application')
% Pass 'oApp' to exFunction
R = fhun(5,oApp)
end
end
matlabpool close

更多回答(1 个)

Edric Ellis
Edric Ellis 2011-10-14
As you've discovered, there are some limitations with passing handle-type objects into the body of SPMD blocks. This is because the workers executing the SPMD block are separate processes, potentially on separate machines. Therefore the data is effectively saved and reloaded (but without going via the disk).
Perhaps you could use my 'WorkerObjectWrapper' which is designed to help managing the lifetime of objects to be used within SPMD and PARFOR.

Community Treasure Hunt

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

Start Hunting!

Translated by