我想将visual-studio 2012中用c#编写的winform编译成一个dll然后加载到matlab 2013a中。使用matlab .net接口,然后我想与winform交互,听取它的事件并通过一组预定义的公共方法传递数据。我正在使用Windows 7 Ultimate SP2。
这非常好用,我可以与所有原生的winform工具,按钮,树,面板甚至图表进行交互。但是我想使用ILnumerics,尤其是用于显示"场景的ILpanel。包含所有奇迹的东西。这是我打砖墙时,在编译为dll并调用matlab时,IPanel中没有任何内容呈现。它只显示默认的椭圆形。
我可以将matlab作为一个过程添加到visual studio中并运行代码。这一切都很好。看起来第32行的场景未正确连接到iLPanel1。
任何帮助都将不胜感激。
Form1.cs 没有form1.Designer.cs的主要c#代码
using System;
using System.Windows.Forms;
using ILNumerics;
using ILNumerics.Drawing.Plotting;
using ILNumerics.Drawing;
using MarkerStyle = ILNumerics.Drawing.MarkerStyle;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void PlotData(double[,] myX)
{
var myDoubleVec = new double[myX.Length];
for (int i = 0; i < myX.Length; i++)
{
myDoubleVec[i] = myX[i, 0];
}
var scene = new ILScene();
ILArray<double> myNumX = myDoubleVec;
scene.Add(new ILPlotCube {
new ILLinePlot(ILMath.tosingle(myNumX.T),
markerStyle: MarkerStyle.Dot)
});
ilPanel1.Scene = scene;
}
private void ilPanel1_Load_1(object sender, EventArgs e)
{
var myDouble = new double[,] { { 2 }, { 4 }, {9 }, { 16 } }; ;
PlotData(myDouble);
}
public void PlotRandom()
{
double yValue = 50.0;
double yValue2 = 200.0;
if (chart1.Series["Series1"].Points.Count > 0)
{
yValue = chart1.Series["Series1"].Points[chart1.Series["Series1"].Points.Count - 1].YValues[0];
yValue2 = chart1.Series["Series2"].Points[chart1.Series["Series1"].Points.Count - 1].YValues[0];
}
Random random = new Random();
for (int pointIndex = 0; pointIndex < 50; pointIndex++)
{
yValue = yValue + (float)(random.NextDouble() * 10.0 - 5.0);
chart1.Series["Series1"].Points.AddY(yValue);
yValue2 = yValue2 + (float)(random.NextDouble() * 10.0 - 5.0);
chart1.Series["Series2"].Points.AddY(yValue2);
}
}
private void button1_Click(object sender, EventArgs e)
{
PlotRandom();
}
private void button2_Click(object sender, EventArgs e)
{
var myDouble = new double[,] { { 2 }, { 4 }, { 6 }, { 8 } }; ;
PlotData(myDouble);
}
}
}
得到的winform如下所示。
用于加载程序集和操作表单的Matlab代码。
NET.addAssembly('C:\Users\philliproso\Documents\Visual Studio 2012\Projects\WindowsFormsApplication3\WindowsFormsApplication3\bin\Debug\WindowsFormsApplication3.dll')
myForm=WindowsFormsApplication3.Form1;
myForm.Show;
myForm.plotRandom; %this call works fine
myForm.PlotData(rand(50,1)); %this call has no effect