Why do I see "The RPC server is unavailable" or "The remote procedure call failed" when calling MATLAB from a C# client?
12 views (last 30 days)
Show older comments
MathWorks Support Team
on 11 Mar 2022
Edited: MathWorks Support Team
on 29 Feb 2024
I am currently following a workflow similar to the one outlined in the following documentation:
Please run the below command in the MATLAB R2018b command window to get the release specific documentation
web(fullfile(docroot, 'matlab/matlab_external/call-matlab-function-from-c-client.html'))
My workflow evokes a routine that executes MATLAB functions (this is done using "matlab.Execute()") in a loop. Each time I call my routine in my for loop, I open a MATLAB session using "MLApp.MLApp matlab = new MLApp.MLApp();". When I wrap up with my MATLAB session I use "matlab.Quit();" to close it.
Essentially my workflow is repeatedly doing the following in a for-loop: creating a MATLAB instance, executing MATLAB functions on that MATLAB instance, and then closing that MATLAB instance.
When executing my workflow in my C# client, I am consistently running into errors that say "The RPC server is unavailable" and "The remote procedure call failed". How can I resolve these errors?
Accepted Answer
MathWorks Support Team
on 18 Jan 2024
Edited: MathWorks Support Team
on 29 Feb 2024
The reason that you are seeing the "The RPC server is unavailable" and "The remote procedure call failed" errors is that the execution of MATLAB functions from C# clients requires opening MATLAB as a COM server. It takes time for COM servers to quit, and if you try to create a new MATLAB COM server while the previous one is being terminated, the new COM server will get attached to the old COM server. This issue is exasperated because you are opening and closing your MATLAB instance in a for loop, which does not give enough time for the MATLAB instance to close. This eventually results in the error that you are seeing.
The solution is to use one MATLAB instance instead of creating a new one in each iteration of your loop. If you would like to use the approach where you create a MATLAB instance every iteration of your loop, you must put a sleep after trying to close the MATLAB instance so it has time to close.
Please use the below link to search for the required information in the current release:
https://www.mathworks.com/help/
0 Comments
More Answers (0)
See Also
Categories
Find more on Write COM Applications to Work with MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!