我在C#Visual Studio中有以下代碼。我試圖通過COM服務器使用MATLAB繪製數據。我能夠做到這一點,但有了我的代碼,MATLAB輸出控制檯只顯示變量「數字」的最後一個值,當它繪製時,它將覆蓋所有以前的值。另外,我如何製作可變大小的動態數據呢?我的目標是實時繪製數據。任何建議,將不勝感激!C#我的ArrayList只包含最後一個項目
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
////// Create the MATLAB instance
MLApp.MLApp matlab = new MLApp.MLApp();
for (int j = 1; j <= 10; j++)
{
int[] numbers = new int[11];
numbers[j] = j * 15;
matlab.Execute("numbers");
matlab.PutWorkspaceData("A", "base",numbers);
matlab.Execute("plot(A);");
}
// //Console.WriteLine(j*15);
//}
//Console.ReadKey();
}
}
}
UPDATE:
var numbers = new List<int>();
for (int j = 1; j <= 10; j++)
{
//numbers[j] = j * 15;
int val= j * 15;
numbers.Add(val);
var array = numbers.ToArray();
matlab.Execute("array");
matlab.PutWorkspaceData("A", "base", array);
matlab.Execute("plot(A);");
}
是@ Sabi3023已經提供的解。你會碰巧知道如何在這一行中使數組大小爲任意或動態: int [] numbers = new int [11];即。將11更改爲動態值 – DashD
@DashD,請參閱更新回答 – Sajal
是的,我之前曾嘗試過這種方法,但它會導致錯誤。 – DashD