Why do I receive a System.Run​time.Inter​opServices​.SEHExcept​ion error?

19 次查看(过去 30 天)
While trying to run the sample code below from Matlab an exception is encountered ('External component has thrown an exception.') at the following line: using (dynamic eng = MATLABEngine.StartMATLAB())
The stackTrace shows:
at MathWorks.MATLAB.Types.SafeHandles.ArrayFactoryHandle.array_factory_create_v2(ArrayFactoryHandle& factory)
at MathWorks.MATLAB.Types.MATLABArrayFactory.get_Default()
at MathWorks.MATLAB.Engine.MATLABEngineFactory.get_Default()
at MathWorks.MATLAB.Engine.MATLABEngine.StartMATLAB()
at MathWorks.MATLAB.Engine.ConsoleExamples.Program.Main(String[] args) in C:\Users\jtaranto\source\repos\CsharpDotNetExample\console\Program.cs:line 24
/* Copyright 2022 The MathWorks, Inc. */
/*
* This file contains is the entry point to the Console Application. It does the following:
* 1. Starts a MATLAB instance and connects to it.
* 2. Runs all of the example files included in this project.
* 3. Closes and disposes of MATLAB.
*/
using System;
namespace MathWorks.MATLAB.Engine.ConsoleExamples
{
public class Program
{
static readonly string LineBreak = new string('=', 20);
/// <summary>
/// Entry point which runs all .NET Engine examples.
/// </summary>
public static void Main(string[] args)
{
// Wrap in a `using` block to ensure proper disposal of unmanaged resources.
Console.Write("Starting MATLAB... ");
using (dynamic eng = MATLABEngine.StartMATLAB())
{
Console.WriteLine("done.");
Console.WriteLine("BASIC EXAMPLE");
Console.WriteLine(LineBreak);
BasicExample.Run(eng);
Console.WriteLine(LineBreak);
Console.WriteLine("\nWORKSPACE EXAMPLE");
Console.WriteLine(LineBreak);
WorkspaceExample.Run(eng);
Console.WriteLine(LineBreak);
Console.WriteLine("\nOBJECT EXAMPLE");
Console.WriteLine(LineBreak);
ObjectExample.Run(eng);
Console.WriteLine(LineBreak);
Console.WriteLine("\nEXCEPTION EXAMPLE");
Console.WriteLine(LineBreak);
ExceptionExample.Run(eng);
Console.WriteLine(LineBreak);
}
// MATLABEngine.Dispose() is implicitly called when "eng" goes out of scope.
Console.WriteLine("\nMATLAB instance \"eng\" has been closed.");
// Call when you no longer need MATLAB Engine in your application.
MATLABEngine.TerminateEngineClient();
Console.WriteLine("\nMATLAB can no longer be accessed from this app.");
}
}
}

回答(1 个)

Sugandhi
Sugandhi 2023-10-20
Hi,
I understand that you are encountering exceptions thrown while trying to run the sample code from Matlab.
The exception "External component has thrown an exception" at the line
using (dynamic eng =MATLABEngine.StartMATLAB())
indicates that there is an issue with the MATLAB Engine or its dependencies.
Few possible workarounds could be:
  1. Ensure that the versions of MATLAB and the .NET Framework you are using are compatible. Make sure you are using a supported version of MATLAB and the corresponding version of the .NET Framework.
  2. Double-check that MATLAB is installed correctly on your system and is functioning properly. Launch MATLAB independently and verify that it starts without any errors.
  3. Ensure that all the necessary dependencies for running MATLAB Engine with .NET are installed. These dependencies include the Visual C++ Redistributable packages and the .NET Framework. Make sure you have the correct versions installed.
  4. Try restarting your computer to ensure a clean state. Sometimes, a restart can resolve temporary issues or conflicts that may be causing the exception.
  5. If the issue persists, consider reinstalling the MATLAB Engine for .NET. Uninstall the existing MATLAB Engine and then reinstall it using the MATLAB installer.

类别

Help CenterFile Exchange 中查找有关 Startup and Shutdown 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by