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

조회 수: 19 (최근 30일)
John Taranto
John Taranto 2022년 10월 4일
답변: Sugandhi 2023년 10월 20일
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