Reputation: 17388
It seems to be impossible to invoke matlab several times:
[STAThread]
static void Main(string[] args)
{
IList<DTO> LotsOfWork = new List<DTO>();
// create some work
for(int c = 0; c < 10; c++)
{
LotsOfWork.Add(new DTO(){ Id = c, Parameter1 = c, Parameter2 = c });
}
// deal with work
foreach (DTO DTO in LotsOfWork)
{
MLApp.MLApp matlab = new MLApp.MLApp();
object result;
matlab.Execute("clear;");
matlab.PutWorkspaceData("a", "base", DTO.Parameter1);
matlab.PutWorkspaceData("b", "base", DTO.Parameter2);
matlab.Execute("result = a + b;");
matlab.GetWorkspaceData("result", "base", out result);
}
}
public class DTO
{
public int Id { get; set; }
public double Parameter1 { get; set; }
public double Parameter2 { get; set; }
public string Result { get; set; }
}
The second loop iteration throws an exception:
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at MLApp.DIMLApp.GetWorkspaceData(String Name, String Workspace, Object& pdata)
at Sandbox.Program.Main(String[] args) in C:\Bla\Program.cs:line 53
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
I am also wondering whether it is possible to run something like this 'threaded' (i.e. spawn off a thread for each DTO object). Thanks.
Upvotes: 0
Views: 813
Reputation: 17388
Solution. Replace:
object result;
with:
object result = null;
This is Mathworks reponse:
This error is caused by the fact that GetWorkspaceData expect an empty VARIANT as output parameter but after running the loop once, result has actually become a double with value 0.0. So you would need to change your loop into:
foreach (DTO DTO in LotsOfWork)
{
object result = null; //Initialize as null
matlab.Execute("clear;");
matlab.PutWorkspaceData("a", "base", DTO.Parameter1);
matlab.PutWorkspaceData("b", "base", DTO.Parameter2);
matlab.Execute("result = a + b;");
matlab.GetWorkspaceData("result", "base", out result);
}
Upvotes: 2