- 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.
- 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.
- 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.
- Try restarting your computer to ensure a clean state. Sometimes, a restart can resolve temporary issues or conflicts that may be causing the exception.
- If the issue persists, consider reinstalling the MATLAB Engine for .NET. Uninstall the existing MATLAB Engine and then reinstall it using the MATLAB installer.
Why do I receive a System.Runtime.InteropServices.SEHException 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.");
}
}
}
댓글 수: 0
답변 (1개)
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:
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!