开发者

C# calls MATLAB

I am following Matlab's example to run in C# but as I am new to C#, I would like to print the results of System.Array prresult = new double[4]; here is the C# code that uses MATLAB:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1{
class Class1{
    [STAThread]
    static void Main(string[] args){
        MLApp.MLAppClass matlab = new MLApp.MLAppClass();
        System.Array pr = new double[4];
        pr.SetValue(11,0);
        pr.SetValue(12,1);
        pr.SetValue(13,2);
        pr.SetValue(14,3);
        System.Array pi = new double[4];
        pi.SetValue(1,0);
        pi.SetValue(2,1);
        pi.SetValue(3,2);
        pi.SetValue(4,3);
        开发者_C百科matlab.PutFullMatrix("a", "base", pr, pi);
        System.Array prresult = new double[4];
        System.Array piresult = new double[4];
        matlab.GetFullMatrix("a", "base", ref prresult, ref piresult);

        }   
    }
}

I added these lines before and after like:

    System.Array prresult = new double[4];
    System.Array piresult = new double[4];
    Console.Write(prresult);
    Console.Write(piresult);
    matlab.GetFullMatrix("a", "base", ref prresult, ref piresult);
    Console.Write(prresult);
    Console.Write(piresult);

I am getting in the console:

System.Double[]System.Double[]System.Double[]System.Double[] . . .

How do I print in console the right results??


Something like this:

foreach(var item in prresult)
{
   Console.Write(item.ToString() + ", ");
}

Here's a function to output the array elements in MATLAB-like syntax:

static void PrintArray(double[] aArray)
{
    var str = "";
    for (int index = 0; index < aArray.Length; index++)
    {
        var item = aArray[index];
        str += item.ToString();
        if (index < aArray.Length - 1)
            str += ", ";
    }
    Console.WriteLine("[" + str + "]");
}

So it can be used like this:

PrintArray(prresult);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜