簡體   English   中英

在C#中使用參數調用Matlab函數

[英]call matlab function with parameters in c#

我創建了這樣的matlab函數

function max = mymax(n1,n2,n3,n4,n5)
%This function calculates the maximum of the
% five numbers given as input
max =  n1;
if(n2 > max)
   max = n2;
end
if(n3 > max)
   max = n3;
end
if(n4 > max)
max = n4;
end
if(n5 > max)
   max = n5;
end

end

然后我從其中引用了我的C#Windows窗體對象的.dll。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Mymax;
using MathWorks.MATLAB.NET.Arrays;
using MathWorks.MATLAB.NET.Utility;

namespace Mymax_with_Matlab
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Mymax.Mymax c1 = new Mymax.Mymax();
            int a = Convert.ToInt32(c1.mymax(1, 2, 3, 4, 5));

        }
    }
}

mymax是我的dll文件的名稱。 以下是導致我的應用停止的錯誤:

... MWMCR::EvaluateFunction error ... 
Not enough input arguments.
Error in => mymax.m at line 14.

... Matlab M-code Stack Trace ...
    at
file C:\Users\milad\AppData\Local\Temp\milad\mcrCache8.1\Mymax_0\Matlab      Examples\mymax.m, name mymax, line 14.

請幫助我....謝謝:)

我發現要將輸入參數傳遞給Matlab編譯的程序集,我需要使用MWArray結構來確保它們是Matlab函數可以使用的格式。 見下文。

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());
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM