I am writing a C# application and I would like to make calls to different matlab functions simultaneously(from different threads). Each Matlab function is located in its own compiled .net library. It seems that I am only able to call one Matlab function at a time however.
ie, if matlab_func1() gets called from thread1 then matlab_func2() gets called from thread2, matlab_func2() must wait for matlab_func1() to finish executing.
Is there a way to call different matlab functions simultaneously? Thanks.
MWArray[] DoKalmanFilter(double vel_x, double vel_y, double vel_z, double cal_x, double cal_y, double cal_z, bool doCal)
{
...set up parameters
ret = KalmanFilter.kalman_actual(6, velx, vely, velz, cal_x, cal_y, cal_z,
return ret;
}
private void DoImageProcessing()
{
..set up parameters
MWArray[] ret = _imgProcessor.DoImageProcessing(2, rgbMarkerColor, hsvThreshold, angleDiffThreshold);
}
I would suggest that when you call your functions, MATLAB (or MATLAB’s real time workshop that gets embedded in to a DLL) is spawned and the function is run. The MATLAB interface between C# and C likely has a few global external variables, and as a result MATLAB probably can’t be spawned twice within the same process. Also, MATLAB itself is in many ways single threaded. They are working on moving towards more use of multi-core.
I have used the Java/Matlab interface before, and read on this site the following:
Matlab is single-threaded. This means that if you try to eval or feval from within a Java function that was called directly from a Matlab function, it will hang waiting for the first matlab function to terminate.
What I might suggest as a test is to put “DoKalmanFilter” and “DoImageProcessing” in to seperate assemblies or applications, and then make an application that spawns your two new seperate applications. This way you get two processes and MATLAB will run once inside each process giving you the ability to run your functions simultaneously.
If that works, you can then start thinking of ways around this. You dont want to have to be spawning applications and closing them down all the time. You might need to use some inter-process communication between your processes to pass data around, it might get ugly 🙂