MATLAB: How to do wait calllib to give answer or catch exception

calllibmatlab haltstrywait

Good day,
I want to fit some parameters (around 50) for a model implemented in a DLL and am using lsqcurvefit. Unfortunately I have noticed Matlab halts sometimes within that optimisation and the only way to resume it is killing Matlab and restart Matlab. Matlab tells me then to have encountered an internal problem. Sometimes it happens on first parameter set, sometimes on 40th. The author of the DLL told me, the model is not very stable and it can happen, that the answer is not an array of double but an array which contains INF entries. I do not know, but I guess this makes Matlab hold.
First idea was to write down every parameter set and get to know the bounds for parameters. But this is not good approach because combination of some parameters may cause this behaviour. Excluding both parameters from parameter space would exclude some parameter combinations which can actually work. Creating such rules of possible combinations would need way too much effort.
Now I am searching, how to wrap a time condition around the calllib command. If time is over and there is no understandable result, the result could be set to zero or excluded. I tried to make a function_handle for that calllib command and use it with wait(fun,'finished',1). But "wait" does not work with function_handles. How can I put the command into a job to use it with wait? Or is there a way to remove the DLL job if failed and prevent Matlab from halting? I do not know, whether such approach could work with a DLL but basically I am thinking of something simple like:
try('time',1)
[answer1, answer2 , answer3] = calllib(DLL,'function',length,array1, array2 ,array3);
catch
[answer1, answer2 , answer3] = [zeros(1,length), zeros(1,length) , zeros(1,length)];
end
Thanks for you help!
Tom

Best Answer

No, you can't do something like that in matlab and even in languages were you have access to the OS API it wouldn't be trivial to code without risking to crash the application.
Dlls are highly privileged, when you load a dll (with loadlibrary in matlab) they become an integral part of your application (so a part of matlab executable in your case). That makes them very useful but also means that if they go wrong they take the application down with them.
Ultimately the problem is with your dll. I'm afraid you'll have to go back to the author and ask them to improve their code. A dll that is not stable will crash whichever program is calling it. The author of that dll could implement the timer you suggest in the dll to stop the processing. Depending on the language they've used to write the dll, they will have to ensure that when they interrupt the processing, the dll is in a stable state. For some languages, this would be taken care of for them.