MATLAB .NET程序集(未定义函数或变量“feedforwardnet”)

时间:2021-11-22 19:58:55

I'm trying to integrate MATLAB 2010b with Visual Studio 2008 Professional.

我正在尝试将MATLAB 2010b与Visual Studio 2008专业人员集成。

I have the following MATLAB method.

我有下面的MATLAB方法。

function varargout = perform( func, varargin )

%% // Set default values
workspaceDirectory = ['Results/MatlabWorkspace_' datestr(now, 'yyyy-mm-dd_HH-MM-SS')];

clear args
args = struct('workspacePath', [ workspaceDirectory '/workspace.mat'], ...
              'testArray', [], ...
              'k', 50, ...
              'rate', 0.0001, ...
              'trainingDataPath', 'P2AT_LaserImageVectorList.csv', ...
              'network', 'feedforwardnet', ...
              'initialWeights', [], ...
              'divideFcn', 'dividerand', ...
              'trainRatio', 70/100, ...
              'testRatio', 15/100, ...
              'valRatio', 15/100, ...
              'trainInd', [], ...
              'testInd', [], ...
              'valInd', [], ...
              'trainFcn', 'trainlm', ...
              'performFcn', 'mse', ...
              'biasConnect', [0; 0; 0], ...
              'layerSize', [9; 4; 1], ...
              'layerTransferFcn', ['tansig '; 'tansig '; 'purelin'], ...
              'max_fail', 10, ...
              'mu_dec', 0.04, ...
              'useInitialWeights', false, ...
              'saveResults', true);

% // Returns a modified properties structure
args = getopt(args,varargin);

args.layerTransferFcn = cellstr(args.layerTransferFcn);

if args.saveResults && ~strcmpi(func,'test')
    if (exist(workspaceDirectory, 'dir') ~= 7)
        mkdir(workspaceDirectory);
    end
end

if (strcmpi(func,'test'))
    try
        load(args.workspacePath, '-regexp', '.*');
    catch err
        Warning(err.message);
        varargin{1,1} = null;
        return;
    end

    data_inputAngle = args.testArray(2501);
    data_inputPCA = args.testArray(1:2500);

    if size(data_inputPCA,1) == 1
        data_inputPCA = data_inputPCA';
    end

    switch(featureExtractionMethod)
        case {'gha','apex'}
            % // [W, errvals] = gha(data_inputPCA, k, varargin{1,3});
            data_PCs  = W' * data_inputPCA;
            data_inputNN = [data_PCs; data_inputAngle];

        case 'nnmf'
            % // [W,H,D] = nnmf(data_inputPCA',k);
            data_PCs  = H * data_inputPCA;
            data_inputNN = [data_PCs; data_inputAngle];

        case 'pcaProcess'

        otherwise
            warning = 'ERROR: no feature extraction method has been defined.';
            Warning('ERROR: no feature extraction method has been defined.');
            varargout{1,1} = null;
            return;
    end

    % // Just to test to see if it recognizes 'feedforwardnet'.
    testnet = feedforwardnet;    % // <------------------------------- LINE 81

    % // Saving all the workspace variables to see if they are all correctly processed.
    save('all');

    varargout{1,1} = net(data_inputNN); %// <------------------------- LINE 86

    end
end

And this is how I create my DLL file to import in Visual Studio:

这是我在Visual Studio中创建DLL文件的方法:

%%// Determine path names
workdir = pwd();
outdir = fullfile(workdir, 'Output');
dnetdir = fullfile(workdir, 'dotnet');

%%// Determine file names
mfile = fullfile(workdir, 'perform.m');
dnetdll = fullfile(dnetdir, 'dotnet.dll');

%%// Build .NET Assembly
eval(['mcc -N -d ''' dnetdir ''' -W ''dotnet:dotnet,' ...
        'dotnetclass,0.0,private'' -T link:lib ''' mfile '''']);

So everything works perfectly fine when I use MATLAB Engine's COM interface to run the routine inside MATLAB from C#:

因此,当我使用MATLAB引擎的COM接口在MATLAB中运行c#中的程序时,一切都很正常。

/*
 * This function calls the routine inside
 * MATLAB using the MATLAB Engine's COM interface
 */
static private float MatlabTestDebug(float[] testData, Double targetAngle)
    {
        Array X = new double[testData.Length + 1];
        testData.CopyTo(X, 0);
        X.SetValue((double)targetAngle, testData.Length);

        Array zerosX = new double[X.GetLength(0)];

        MLApp.MLAppClass matlab = new MLApp.MLAppClass();

        matlab.PutFullMatrix("testArray", "base", X, zerosX);
        matlab.PutWorkspaceData("workspacePath", "base", "workspace.mat");

        // Using Engine Interface, execute the ML command
        // contained in quotes.
        matlab.Execute("cd 'c:\\Users\\H\\Documents\\Project\\Source Code\\MatlabFiles';");
        matlab.Execute("open perform.m");
        matlab.Execute("dbstop in perform.m");
        matlab.Execute("result = perform('test', 'workspacePath', 'workspace.mat', 'testArray', testArray);");
        matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll");

        return (double)matlab.GetVariable("result", "base");
    }

But when I use the .NET assembly, it's not recognizing 'feedforwardnet'. I used to get an error on line 86 (net(data_inputNN)). So I added a line to test to see if it at least recognizes 'feedforwardnet', but it didn't.

但是当我使用。net程序集时,它并没有识别“feedforwardnet”。我曾经在第86行(net(data_inputNN)中得到一个错误。所以我添加了一条线来测试它是否至少识别了“feedforwardnet”,但它没有。

Note: I'm loading some variables from a file including "net" which is a neural network (load(args.workspacePath, '-regexp', '.*');)

注意:我正在从一个文件中加载一些变量,包括“net”,它是一个神经网络(args)。workspacePath,“正则表达式”,“。*”);

Also in the MATLAB method if I load a saved "network" from file and then save it to see how it processes the network, it will save it as a "struct" instead of a "network".

同样在MATLAB的方法中,如果我从文件中加载一个保存的“网络”,然后保存它以查看它如何处理网络,它将把它保存为一个“struct”而不是一个“网络”。

I'm assuming it's loading it as a struct to begin with.

我假设它是以结构形式开始的。

I also had this problem inside MATLAB 2009b itself. That's the reason I'm using MATLAB 2010b now, because apparently MATLAB 2009b didn't have this particular neural networks toolbox.

我在MATLAB 2009b本身也有这个问题。这就是我现在使用MATLAB 2010b的原因,因为显然MATLAB 2009b没有这个特殊的神经网络工具箱。

Following is the C# code to use the .NET assembly.

下面是使用. net程序集的c#代码。

/*
 * Calls the method from inside a .NET assembly created with MATLAB
 * using Builder for .NET.
 */
private float MatlabTest(float[] testData, Double targetAngle)
    {
        if (testData != null)
        {
            dotnetclass AClass = new dotnetclass();

            Array X = new double[testData.Length + 1];
            testData.CopyTo(X, 0);
            X.SetValue((double)targetAngle, testData.Length);

            MWNumericArray XNumericArray = new MWNumericArray(X);

            MWArray[] RetVal = AClass.perform(1, "test",
                "workspacePath", "workspace.mat",
                "testArray", XNumericArray);

            Array result = ((MWNumericArray)RetVal[0]).ToVector(MWArrayComponent.Real);

            return (float)result.GetValue(0);
        }
        else
        {
            return 0f;
        }
    }

I'm getting this error in Visual Studio:

我在Visual Studio中得到这个错误:

... MWMCR::EvaluateFunction error ...
Undefined function or variable 'feedforwardnet'.
Error in => perform.m at line 81.

NOTE: version of my compiler and softwares:

注:我的编译器和软件版本:

Compiler: Microsoft Visual C++ 2008 SP1 in C:\Program Files (x86)\Microsoft Visual Studio 9.0

编译器:微软Visual c++ 2008 SP1 C:\Program Files (x86)\Microsoft Visual Studio 9.0

MATLAB: R2010b (64-bit)

MATLAB:R2010b(64位)

Visual Studio: MVS 2008 professional (.NET Framework 3.5 SP1)

Visual Studio: MVS 2008 professional(。NET Framework 3.5 SP1)

Microsoft Windows SDK 6.1

Microsoft Windows SDK 6.1


Recent Updates:

最近更新:

I've added the path of the neural network toolbox in mcc.

我在mcc中添加了神经网络工具箱的路径。

eval(['mcc -N -p ''C:\Program Files\MATLAB\R2010b\toolbox\nnet'' -d ''' dnetdir ''' -W ''dotnet:dotnet,' ...
    'dotnetclass,0.0,private'' -T link:lib -v ''' mfile '''']);

Now I get these messages in mccExcludeFiles.log:

现在我在mccdefiles.log中得到这些消息。

C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\@network\network.m

C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\ @network \ network.m

called by C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\nnnetwork\cascadeforwardnet.m (because of toolbox compilability rules)

被C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\nnnetwork\ cascadeforwardnet。m(由于工具箱的可编译性规则)

C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\@network\network.m

C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\ @network \ network.m

called by C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\nnnetwork\feedforwardnet.m (because of toolbox compilability rules)

被C:\Program Files\MATLAB\R2010b\toolbox\nnet\nnet\nnnetwork\ feedforwardnet。m(由于工具箱的可编译性规则)

1 个解决方案

#1


1  

The only answer I could come up with (which not a solution to the problem) was from Creating standalone application that contains neural network toolbox functions, stating that:

我能想到的唯一答案(不是问题的解决方案)是创建一个独立的应用程序,其中包含了神经网络工具箱函数,声明如下:

You will not be able to compile any function which trains the network (like ADAPT). Though the link does not explicitly list these funcions (like ADAPT), they fall under the 'All other command line functionality'.

您将无法编译任何训练网络的函数(如ADAPT)。虽然该链接没有明确列出这些funcions(比如ADAPT),但它们属于“所有其他命令行功能”。

However, you can deploy a M function code which uses a pre-trained network. I believe the SIM function will deploy fine.

但是,您可以部署一个使用预训练网络的M函数代码。我相信SIM功能会很好部署。

The workflow I see is:

我看到的工作流程是:

  1. In MATLAB, train you network using test input/output
  2. 在MATLAB中,使用测试输入/输出来训练网络。
  3. Save the network (MAT file?)
  4. 保存网络(MAT文件?)
  5. Create a deployable function which then uses the pretrained network for new data. The network itself would not change/adapt/train in this function
  6. 创建一个可部署的函数,然后使用预训练的网络进行新数据。网络本身不会改变/适应/训练这个功能。
  7. Compile and deploy the above function
  8. 编译并部署上述函数。

#1


1  

The only answer I could come up with (which not a solution to the problem) was from Creating standalone application that contains neural network toolbox functions, stating that:

我能想到的唯一答案(不是问题的解决方案)是创建一个独立的应用程序,其中包含了神经网络工具箱函数,声明如下:

You will not be able to compile any function which trains the network (like ADAPT). Though the link does not explicitly list these funcions (like ADAPT), they fall under the 'All other command line functionality'.

您将无法编译任何训练网络的函数(如ADAPT)。虽然该链接没有明确列出这些funcions(比如ADAPT),但它们属于“所有其他命令行功能”。

However, you can deploy a M function code which uses a pre-trained network. I believe the SIM function will deploy fine.

但是,您可以部署一个使用预训练网络的M函数代码。我相信SIM功能会很好部署。

The workflow I see is:

我看到的工作流程是:

  1. In MATLAB, train you network using test input/output
  2. 在MATLAB中,使用测试输入/输出来训练网络。
  3. Save the network (MAT file?)
  4. 保存网络(MAT文件?)
  5. Create a deployable function which then uses the pretrained network for new data. The network itself would not change/adapt/train in this function
  6. 创建一个可部署的函数,然后使用预训练的网络进行新数据。网络本身不会改变/适应/训练这个功能。
  7. Compile and deploy the above function
  8. 编译并部署上述函数。