从 c# 调用 Matlab 编译函数时执行错误

本文关键字:执行 错误 函数 编译 调用 Matlab | 更新日期: 2023-09-27 18:34:04

我正在使用 Matlab 2015a,并开发、训练和测试了一个分类集成(提升树(并保存了训练最好的模型(.mat 文件(。

由于我希望在 .Net C# 应用程序中使用此模型,因此我创建了一个 .m 文件来加载包含经过训练的分类器的 .mat 文件,并使用它来根据传入的功能预测结果(请参阅下面的 .m 文件代码(。

function [ypredict score] = fnTrainedClassifer( input_args )
  load ('trainedClassifier.mat');
  [ypredict score] = predict(trainedClassifier,input_args);
end

然后,我使用 Matlab 编译器创建了一个 .Net 程序集,并确保在库运行所需的文件部分中包含 .mat 文件。到目前为止一切都很好...然后,我创建了一个快速的 C# 控制台应用来测试库(请参阅下面的应用代码(并运行它。

using System;
using MathWorks.MATLAB.NET.Arrays;
using TrainedClassifierComp;
namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            MLTestClass theModel = null;   /* Stores deployment class instance */
            MWStructArray inputs = null;   /* Sample input data */
            MWArray[] result = null;       /* Stores the result */
            MWNumericArray prediction = null;  /* Ouptut data extracted from result */
            MWNumericArray score = null;  /* Ouptut data extracted from result */
            /* Create the new deployment object */
            theModel = new MLTestClass();
            /* Create an MWStructArray */
            String[] myFieldNames = { "1", "2", "3", "4", "5", "6", "7", "8" };
            inputs = new MWStructArray(1, 8, myFieldNames);
            /* Populate struct with some sample inputs  */
            inputs["1", 1] = 1;
            inputs["2", 2] = 2;
            inputs["3", 3] = 3;
            inputs["4", 4] = 4;
            inputs["5", 5] = 5;
            inputs["6", 6] = 6;
            inputs["7", 7] = 7;
            inputs["8", 8] = 8;
            /* Show some of the sample data */
            Console.WriteLine("Inputs: ");
            Console.WriteLine(inputs.ToString());
            /* Pass it to a MATLAB function */
            result = theModel.fnTrainedClassifier(1, inputs);
            prediction = (MWNumericArray) result[0];
            score = (MWNumericArray)result[1];
            /* Show the results */
            Console.WriteLine("Prediction: ");
            Console.WriteLine(prediction.ToString());
            Console.WriteLine("Score: ");
            Console.WriteLine(prediction.ToString());
        }
    }
}

不幸的是,我想出了以下执行时间错误,在Google上搜索没有有用的结果:

警告:变量"trainedClassifier"最初保存为 classreg.learning.classif.ClassificationEnsemble 无法实例化为对象,并将作为 uint32 读入。 在 fnTrainedClassifier 中(第 4 行( 使用预测时出错(第 84 行( uint32 类的系统不能与"预测"命令一起使用。首先将系统转换为已识别的模型,例如使用"idss"命令。 fn训练分类器中的错误(第 5 行(

有关如何解决此问题的任何指导,将不胜感激,因为您如何能够实现类似目标。

附加信息:我已经尝试了其他几个AI系统,例如神经网络,但我遇到了同样的问题。在我看来,我没有正确处理这个问题 - 是否有另一种方法可以在外部编译语言(如 c#(中使用 Matlab 函数?

从 c# 调用 Matlab 编译函数时执行错误

这个问题问

了一段时间了,但我遇到了同样的问题。我希望以下解决方案可以帮助一些人节省一些时间。因此,当从 .mat 文件加载类对象时,Matlab 编译器似乎读取类类型但不加载类本身。因此,我们需要以某种方式添加实例化对象的类定义。

为了强制编译器加载所需的类,我创建了该类的一个空对象,然后从 .mat 文件加载对象并将其分配给空对象。您的垫子代码应如下所示;

function [ypredict score] = fnTrainedClassifer( input_args )
     trainedClassifier = classreg.learning.classif.ClassificationEnsemble.empty; % only this line is added.
     load ('trainedClassifier.mat');
     [ypredict score] = predict(trainedClassifier,input_args);
end