MATLABEngi​ne.Connect​MATLAB() executed twice: first time it works, subsequent times - won't work, until you restart the program

3 views (last 30 days)
Setup is windows 11 x64, latest updates, latest Visual Studio Community Edition, etc.
Below behavior reflects in .NET Help Center examples, but it might be easier to follow through with a C# Interactive example:
>
> #r "C:\Program Files\MATLAB\R2024b\extern\dotnet\netstandard2.0\MathWorks.MATLAB.Engine.dll"
. #r "C:\Program Files\MATLAB\R2024b\extern\dotnet\netstandard2.0\MathWorks.MATLAB.Types.dll"
. using MathWorks.MATLAB.Engine;
. using MathWorks.MATLAB.Exceptions;
. using MathWorks.MATLAB.Types;
> string[] mSessions = MATLABEngine.FindMATLAB();
.
> mSessions
string[1] { "netinstance" } #// a Matlab session is already opened, and shared with name "netinstance". We can connect into it just fine:
> dynamic matlab = MATLABEngine.ConnectMATLAB("netinstance");
.
> matlab.cd("c:/temp"); #// executed successfully
>
#// for whatever reason, the reference to matlab can be lost, or we need to connect again from another thread, or we recompile the program, or so many other reasons /etc.
> matlab = null; #// let's assume we are in another thread or for whatever reason, we just don't have access to matlab variable anymore
#// let's try to reconnect:
> dynamic matlab = MATLABEngine.ConnectMATLAB("netinstance");
.
MathWorks.MATLAB.Exceptions.MATLABNotAvailableException: An unexpected error occurred.
+ MathWorks.MATLAB.Engine.Impl.MATLABEngineFactoryImpl.ThrowOnErrFlag(bool) at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/Impl/MATLABEngineFactoryImpl.cs : 46
+ MathWorks.MATLAB.Engine.Impl.MATLABEngineFactoryImpl.ConnectMATLAB(string) at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/Impl/MATLABEngineFactoryImpl.cs : 118
+ MathWorks.MATLAB.Engine.MATLABEngine.ConnectMATLAB(string) at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/MATLABEngine.cs : 118
#// the session is still there, we can "find" it:
> string[] mSessions = MATLABEngine.FindMATLAB();
> mSessions
string[1] { "netinstance" }
>
#// however, we cannot connect to it anymore:
> dynamic matlab = MATLABEngine.ConnectMATLAB("netinstance");
MathWorks.MATLAB.Exceptions.MATLABNotAvailableException: An unexpected error occurred.
+ MathWorks.MATLAB.Engine.Impl.MATLABEngineFactoryImpl.ThrowOnErrFlag(bool) at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/Impl/MATLABEngineFactoryImpl.cs : 46
+ MathWorks.MATLAB.Engine.Impl.MATLABEngineFactoryImpl.ConnectMATLAB(string) at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/Impl/MATLABEngineFactoryImpl.cs : 118
+ MathWorks.MATLAB.Engine.MATLABEngine.ConnectMATLAB(string) at /mathworks/devel/bat/filer/batfs2552-0/BR2024bd.2712019.BR2024bd.2702509.pass/build/matlab/external/engines/dotnet/dotnet_engine/Engine/MATLABEngine.cs : 118
>
The same thing happens if you run:
MATLABEngine.TerminateEngineClient();
Once you did that, you cannot run StartMATLAB or ConnectMATLAB anymore as defined under:
Until you restart the program fully.
This doesn't make much sense ... you should connect multiple times into a a shared instance from within different threads part of the same program.
It is impossible to expect that you should connect into "netinstance" just ONCE for the duration of a program execution ...
What's missing, is there anything wrong in the above, or should there be more?
It's impossible to work with this in Visual Studio 2022 - for any new Build/Compile/Run, you just need to restart Visual Studio ... as you lost access to dynamic variable matlab ...

Answers (1)

Divyam
Divyam on 17 Oct 2024
Hi @Andy,
You cannot connect to the same shared MATLAB session twice. This is because the "StartMATLAB" function returns a "unique_ptr" object in C#, which disposes of the object in the background once the pointer has been released either by assigning it a null value or by terminating the engine client.
It is possible that the "FindMATLAB" function will still return the same shared MATLAB session after the pointer has been released, but trying to connect to it with the "ConnectMATLAB" function will cause an error or crash the application. This is because the session of MATLAB is closing in the background.
You can open a shared MATLAB session in the command window with the command:
matlab -r matlab.engine.shareEngine
The session pointer obtained by the "FindMATLAB" and "ConnectMATLAB" workflow can now be disposed and added again. The overall workflow here would be to either manually open the shared session of MATLAB from the command line or automate it with a .bat script and call it with the system calls in C#.
For more information regarding opening a shared MATLAB session in the command window, refer to this documentation: https://www.mathworks.com/help/matlab/matlab_external/connect-to-existing-net-shared-session.html

Community Treasure Hunt

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

Start Hunting!