static void Main(string[] args) { // Start the Configuration Manager //ConfigurationManager configManager = new ConfigurationManager(@"D:\Work\Research\PhD\Implementation\Diactrization\Preprocessing\Preprocessing\Configurations.xml"); ConfigurationManager configManager = new ConfigurationManager(args[0]); // Start the logger Logger logger = new Logger(configManager); // Start the parser Parser parser = new Parser(configManager, logger); // Start Train Set parsing from root directory parser.Parse(configManager.rootTrainDirectory, "Train"); // Start Test Set parsing from root directory parser.Parse(configManager.rootTestDirectory, "Test"); // Copy files to configuration environment if required if (configManager.configEnvDirectory != "") { MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.Execute(@"load('" + configManager.rootTrainDirectory + @"\input_data');"); matlab.Execute(@"load('" + configManager.rootTestDirectory + @"\input_data');"); matlab.Execute(@"save('" + configManager.configEnvDirectory + @"\input_data');"); } }
static private void UseEngine(double[,] ar, ref Array cr, ref Array ci) { // Instantiate MATLAB Engine Interface through com Console.WriteLine("Matlab Startup...\r\n"); MLApp.MLAppClass matlab = new MLApp.MLAppClass(); // Make imaginary matricies double[] a_d = new double[] { 68.8, 120, 110, 118, 150, 179, 180, 150, 178, 152, 163, 107, 149, 97.8, 90.1, 94.3, 90.9, 89.9, 53.4 }; matlab.PutFullMatrix("a", "base", a_d, new double[19]); String MyDocs = Environment.GetFolderPath(Environment.SpecialFolder.Personal); String ProjectLocation = "Visual Studio 2010\\Projects\\Kinect-Tracking-Project\\MatlabPrototypes\\FeatureDetection"; String matFileCD_command = String.Format("cd '{0}';", System.IO.Path.Combine(MyDocs, ProjectLocation)); Console.WriteLine(matFileCD_command); matlab.Execute(matFileCD_command); // matlab.Execute("open testBayes.m"); // matlab.Execute("dbstop in math_on_numbers.m"); Console.WriteLine("Matlab Processing...\r\n"); matlab.Execute("c = transpose(testBayes(a));"); //matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll"); //matlab.Execute("dbquit all"); try { matlab.GetFullMatrix("c", "base", ref cr, ref ci); } catch (Exception) { Console.WriteLine("someErr"); } }
public static void seeGraph(string folderName, int args) { MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.PutWorkspaceData("Graphargs", "base", args); matlab.PutWorkspaceData("folderName", "base", folderName); matlab.Execute("cd 'D:\\Dropbox\\Research Assistant\\SIBI Data Feature\\Code\\kencoba\\'"); matlab.Execute("FeatureLooker(folderName,Graphargs);"); //matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll"); }
public override void Run() { MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.Visible = 1; //会显示Command Window matlab.Execute("clc; clear all; close all;"); Project project = Solution.Projects[0]; int line = 0; for (int length1 = 3; length1 <= 7; length1++) { for (int length2 = 3; length2 <= 7; length2++) { if (length2 > length1) { // set new parameters project.Parameters["Length1"].Value = length1; project.Parameters["Length2"].Value = length2; // print parameters Console.Write("Length1 = " + length1 + " Length2 = " + length2); // start backtest Start(); // calculate objective function double objective = Solution.Portfolio.GetValue(); // print objective Console.WriteLine(" Objective = " + objective); // check best objective matlab.Execute(string.Format("x({0})={1}; y({0})={2}; z({0})={3};", ++line, length1, length2, objective)); } } } matlab.Execute("t1=linspace(min(x),max(x),max(x)-min(x)+1); %% 如果数据不多,最后的max(x)-min(x)+1可以改成50等"); matlab.Execute("t2=linspace(min(y),max(y),max(y)-min(y)+1); %% 同上"); matlab.Execute("[X,Y]=meshgrid(t1,t2);"); matlab.Execute("Z=griddata(x,y,z,X,Y,'v4');"); matlab.Execute("figure,surfc(X,Y,Z),colorbar"); matlab.Execute("xlabel('x'); ylabel('y'); zlabel('z');"); matlab.Execute("title('Performance');"); matlab.Execute("save"); //保存一下,用于后期分析 // 执行完后暂时不退出 //matlab.Quit(); //matlab = null; }
static private double[] scores(DataTable kinectTable, double[] groundPlane, string exercise) { double[,] kinectData = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[,] kinectZeros = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[] groundPlaneZeros = new double[4]; System.Array cr = new double[3]; System.Array ci = new double[3]; for (int r = 0; r < kinectTable.Rows.Count; r++) { for (int c = 0; c < kinectTable.Columns.Count; c++) { kinectData[r, c] = (double)kinectTable.Rows[r][c]; } } MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.PutFullMatrix("CS_kinectData", "base", kinectData, kinectZeros); matlab.PutFullMatrix("CS_groundPlane", "base", groundPlane, groundPlaneZeros); String MyDocs = Environment.GetFolderPath(Environment.SpecialFolder.Personal); String ProjectLocation = "Visual Studio 2010\\Projects\\Kinect-Tracking-Project\\MatlabPrototypes\\FeatureDetection"; String matFileCD_command = String.Format("cd '{0}';", System.IO.Path.Combine(MyDocs, ProjectLocation)); matlab.Execute(matFileCD_command); if (exercise == "squats") { matlab.Execute("c = cs_matlab_classifier(CS_kinectData, CS_groundPlane, 'squats');"); } else if (exercise == "arm raise") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'arm raise'));"); } else if (exercise == "leg raise") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'leg raise'));"); } else if (exercise == "leg extension") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'leg extension'));"); } matlab.GetFullMatrix("c", "base", cr, ci); double[] cr_d = new double[3]; cr_d = (double[])cr; return(cr_d); }
public static void testOutput() { MLApp.MLAppClass matlab = new MLApp.MLAppClass(); int a = 2; int b = 6; matlab.PutWorkspaceData("a", "base", a); matlab.PutWorkspaceData("b", "base", b); matlab.Execute("cd 'D:\\Dropbox\\Research Assistant\\SIBI Data Feature\\Code\\kencoba'"); matlab.Execute("c = a + b"); matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll");; }
static void Main(string[] args) { MLApp.MLAppClass matlab = new MLApp.MLAppClass(); // create variables: a_0, a_1, ..., a_4 for (int k = 0; k < 5; k++) { matlab.Execute(string.Format("a_{0} = rand(2);", k)); } // retrieve variables from MATLAB and print their contents object a; for (int k = 0; k < 5; k++) { // current variable name string varname = string.Format("a_{0}", k); // get data array a = null; // without this line, an exception is thrown! matlab.GetWorkspaceData(varname, "base", out a); // print contents var arr = (double[, ])a; Console.WriteLine("\nndims(a) = {0}, numel(a) = {1}", arr.Rank, arr.Length); for (int i = 0; i < arr.GetLength(0); i++) { for (int j = 0; j < arr.GetLength(1); j++) { Console.WriteLine("{0}[{1},{2}] = {3}", varname, i, j, arr[i, j]); } } } }
public override void Run() { MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.Visible = 1; //会显示Command Window matlab.Execute("clc; clear all; close all;"); WatcherStrat(@"D:\test", "*.*"); Project project = Solution.Projects[0]; //int line = 0; for (int length1 = 3; length1 <= 7; length1++) { for (int length2 = 3; length2 <= 7; length2++) { if (length2 > length1) { // set new parameters project.Parameters["Length1"].Value = length1; project.Parameters["Length2"].Value = length2; // print parameters Console.Write("Length1 = " + length1 + " Length2 = " + length2); // start backtest // 这句话要写到matlab中的函数中去 matlab.Execute(@"save D:\test\" + Clock.Now.Ticks); // 等待策略跑完一次 _autoResetEvent.WaitOne(); // calculate objective function double objective = Solution.Portfolio.GetValue(); // print objective Console.WriteLine(" Objective = " + objective); // check best objective } } } // 执行完后暂时不退出 //matlab.Quit(); //matlab = null; WatcherStop(); }
static void Main(string[] args) { // Start the Configuration Manager //ConfigurationManager configManager = new ConfigurationManager(@"D:\Work\Research\PhD\Implementation\Diactrization\Preprocessing\Preprocessing\Configurations.xml"); ConfigurationManager configManager = new ConfigurationManager(args[0]); // Start the logger Logger logger = new Logger(configManager); // Start the train parser Parser trainParser; switch (configManager.trainInputFormat) { case "ReadyFeatures": trainParser = new ReadyFeaturesParser(configManager, logger); break; case "RawTxt": trainParser = new RawTxtParser(configManager, logger); break; default: trainParser = new ReadyFeaturesParser(configManager, logger); break; } // Start Train Set parsing from root directory trainParser.Parse(configManager.rootTrainDirectory, "Train", configManager.trainInputParsingMode, configManager.trainInputFormat); // Start the test parser Parser testParser; switch (configManager.testInputFormat) { case "ReadyFeatures": testParser = new ReadyFeaturesParser(configManager, logger); break; case "RawTxt": testParser = new RawTxtParser(configManager, logger); break; default: testParser = new ReadyFeaturesParser(configManager, logger); break; } // Start Test Set parsing from root directory testParser.Parse(configManager.rootTestDirectory, "Test", configManager.testInputParsingMode, configManager.testInputFormat); // Copy files to configuration environment if required if (configManager.configEnvDirectory != "") { String s = String.Empty; MLApp.MLAppClass matlab = new MLApp.MLAppClass(); s = matlab.Execute(@"load('" + configManager.rootTrainDirectory + @"\input_data');"); if (Regex.Match(s, "Error").Success) { logger.LogError(s, ErrorCode.MATLAB_ERROR); } s = matlab.Execute(@"load('" + configManager.rootTestDirectory + @"\input_data');"); if (Regex.Match(s, "Error").Success) { logger.LogError(s, ErrorCode.MATLAB_ERROR); } s = matlab.Execute(@"save('" + configManager.configEnvDirectory + @"\input_data');"); if (Regex.Match(s, "Error").Success) { logger.LogError(s, ErrorCode.MATLAB_ERROR); } } }
// Calling MATLAB function file to perform Gaussian fitting private void GaussianFitting(int n, double[,] arrSamplesR, double[,] arrSamplesI, ref Array arrModes, ref Array arrMUs, ref Array arrSigmaXSigmaY, ref Array junkModes, ref Array junkMUs, ref Array junkSigmaXSigmaY) { // Instantiate MATLAB Engine Interface through com MLApp.MLAppClass matlab = new MLApp.MLAppClass(); //DateTime startTime = DateTime.Now; // Set input matrices matlab.PutFullMatrix("samples", "base", arrSamplesR, arrSamplesI); //DateTime stopTime = DateTime.Now; //TimeSpan duration = stopTime - startTime; //double RunTime = duration.TotalSeconds; //System.Windows.Forms.MessageBox.Show("Set input paramters " + RunTime + " seconds!"); double[] NR = new double[1]; double[] NI = new double[1]; NR[0] = n; NI[0] = 0; matlab.PutFullMatrix("N", "base", NR, NI); //startTime = DateTime.Now; // Using Engine Interface, execute ML script file string appPath = System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath); matlab.Execute("cd " + appPath); matlab.Execute("cd ..\\..\\..\\Scripts"); matlab.Execute("[modes, MUs, SigmaXSigmaY] = IPPAGaussianFitting(samples, N);"); //stopTime = DateTime.Now; //duration = stopTime - startTime; //RunTime = duration.TotalSeconds; //System.Windows.Forms.MessageBox.Show("Execute ML Script " + RunTime + " seconds!"); //startTime = DateTime.Now; // Using Engine Interface, get matrices from the base workspace. matlab.GetFullMatrix("modes", "base", ref arrModes, ref junkModes); matlab.GetFullMatrix("MUs", "base", ref arrMUs, ref junkMUs); matlab.GetFullMatrix("SigmaXSigmaY", "base", ref arrSigmaXSigmaY, ref junkSigmaXSigmaY); //stopTime = DateTime.Now; //duration = stopTime - startTime; //RunTime = duration.TotalSeconds; //System.Windows.Forms.MessageBox.Show("Retrieve matrices " + RunTime + " seconds!"); }
static private string[] scores(DataTable kinectTable, double[] groundPlane, string exercise) { double[,] kinectData = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[,] kinectZeros = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[] groundPlaneZeros = new double[4]; //System.Array cr = new double[3]; //System.Array ci = new double[3]; for (int r = 0; r < kinectTable.Rows.Count; r++) { for (int c = 0; c < kinectTable.Columns.Count; c++) { kinectData[r, c] = (double)kinectTable.Rows[r][c]; } } MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.PutFullMatrix("CS_kinectData", "base", kinectData, kinectZeros); matlab.PutFullMatrix("CS_groundPlane", "base", groundPlane, groundPlaneZeros); String MyDocs = Environment.GetFolderPath(Environment.SpecialFolder.Personal); String ProjectLocation = "Visual Studio 2010\\Projects\\Kinect-Tracking-Project\\MatlabPrototypes\\FeatureDetection"; String matFileCD_command = String.Format("cd '{0}';", System.IO.Path.Combine(MyDocs, ProjectLocation)); matlab.Execute(matFileCD_command); matlab.Execute("initWorkspace();"); if (exercise == "Squat") { matlab.Execute("c = cs_matlab_classifier('squats', CS_kinectData, CS_groundPlane);"); } else if (exercise == "Arm Raise") { matlab.Execute("c = cs_matlab_classifier('armRaise', CS_kinectData, CS_groundPlane);"); } else if (exercise == "Leg Raise") { matlab.Execute("c = cs_matlab_classifier('legRaise', CS_kinectData, CS_groundPlane);"); } else if (exercise == "Hip Abduction") { matlab.Execute("c = cs_matlab_classifier('legExt', CS_kinectData, CS_groundPlane);"); } string[] cr_d = new string[30]; StreamReader FileStreamReader; FileStreamReader = File.OpenText(System.IO.Path.Combine(MyDocs, ProjectLocation) + "\\results.csv"); int i = 0; while (FileStreamReader.Peek() != -1) { string[] words; words = FileStreamReader.ReadLine().Split(','); cr_d[i] = words[0]; cr_d[i + 1] = words[1]; cr_d[i + 2] = words[2]; i = i + 3; } FileStreamReader.Close(); //matlab.GetFullMatrix("c", "base", cr, ci); //double[] cr_d = new double[3]; //cr_d = (double[])cr; return cr_d; }
/* * All input and output arguments must have been allocated * prior to calling these functions * * Input: * ar real part of a * ai imaginary part of a * br real part of b * bi imaginary part of b * * Output: * cr real part of c * ci imaginary part of c * dr real part of d * di imaginary part of d */ static private void UseEngine(Array ar, Array ai, Array br, Array bi, ref Array cr, ref Array ci, ref Array dr, ref Array di) { /* * This function calls the math_by_numbers routine inside * MATLAB using the MATLAB Engine's com interface */ // Instantiate MATLAB Engine Interface through com MLApp.MLAppClass matlab = new MLApp.MLAppClass(); // Using Engine Interface, put the matrix "a" into // the base workspace. // "a" is a complex variable with a real part of ar, // and an imaginary part of ai matlab.PutFullMatrix("a", "base", ar, ai); // Using Engine Interface, put the matrix "b" into // the base workspace. // "b" is a complex variable with a real part of br, // and an imaginary part of bi matlab.PutFullMatrix("b", "base", br, bi); // Using Engine Interface, execute the ML command // contained in quotes. matlab.Execute("cd c:\\demos\\CSharp_MATLAB\\mcode;"); matlab.Execute("open math_on_numbers.m"); matlab.Execute("dbstop in math_on_numbers.m"); matlab.Execute("[c, d] = math_on_numbers(a,b);"); matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll"); //matlab.Execute("dbquit all"); // Using Engine Interface, get the matrix "c" from // the base workspace. // "c" is a complex variable with a real part of cr, // and an imaginary part of ci matlab.GetFullMatrix("c", "base", ref cr, ref ci); // using engine interface, get the matrix "c" from // the base workspace. // "d" is a complex variable with a real part of dr, // and an imaginary part of di matlab.GetFullMatrix("d", "base", ref dr, ref di); }
//// Print out the permutations of the input //static string ShowPermutations<T>(IEnumerable<T> input, int count) //{ // string s = ""; // foreach (IEnumerable<T> permutation in PermuteUtils.Permute<T>(input, count)) // { // foreach (T i in permutation) // { // s += " " + i.ToString(); // } // s += "\n"; // } // return s; //} private static void UseEngine(Array ar, Array ai, Array br, Array bi, ref Array cr, ref Array ci, ref Array dr, ref Array di) { /* * This function calls the math_by_numbers routine inside * MATLAB using the MATLAB Engine's com interface */ // Instantiate MATLAB Engine Interface through com MLApp.MLAppClass matlab = new MLApp.MLAppClass(); // Using Engine Interface, put the matrix "a" into // the base workspace. // "a" is a complex variable with a real part of ar, // and an imaginary part of ai matlab.PutFullMatrix("a", "base", ar, ai); // Using Engine Interface, put the matrix "b" into // the base workspace. // "b" is a complex variable with a real part of br, // and an imaginary part of bi matlab.PutFullMatrix("b", "base", br, bi); // Put test array into base workspace double[,] SR = new double[2, 2]; double[,] SI = new double[2, 2]; SR[0, 0] = 1; SR[0, 1] = 2; SR[1, 0] = 3; SR[1, 1] = 4; SI[0, 0] = 0; SI[0, 1] = 0; SI[1, 0] = 0; SI[1, 1] = 0; System.Array TR = new double[2]; System.Array TI = new double[2]; TR.SetValue(1, 0); TR.SetValue(2, 1); TI.SetValue(0, 0); TI.SetValue(0, 1); // Test List List<double[]> samples = new List<double[]>(); double[] aaa = new double[2]; double[] bbb = new double[2]; aaa[0] = 5; aaa[1] = 6; bbb[0] = 7; bbb[1] = 8; samples.Add(aaa); samples.Add(bbb); double[,] arrSamplesR = new double[samples.Count, 2]; double[,] arrSamplesI = new double[samples.Count, 2]; for (int i = 0; i < samples.Count; i++) { double[] b = samples[i]; arrSamplesR[i, 0] = b[0]; arrSamplesR[i, 1] = b[1]; arrSamplesI[i, 0] = 0; arrSamplesI[i, 1] = 0; } matlab.PutFullMatrix("ss", "base", arrSamplesR, arrSamplesI); // Using Engine Interface, execute the ML command // contained in quotes. matlab.Execute("cd 'C:\\Lanny\\MAMI\\ZPlayGround\\C# Calling MATLAB\\CSharp_MATLAB\\CSharp_MATLAB\\mcode';"); matlab.Execute("open math_on_numbers.m"); matlab.Execute("dbstop in math_on_numbers.m"); matlab.Execute("[c, d] = math_on_numbers(a,b);"); matlab.Execute("com.mathworks.mlservices.MLEditorServices.closeAll"); //matlab.Execute("dbquit all"); // Using Engine Interface, get the matrix "c" from // the base workspace. // "c" is a complex variable with a real part of cr, // and an imaginary part of ci matlab.GetFullMatrix("c", "base", ref cr, ref ci); // using engine interface, get the matrix "c" from // the base workspace. // "d" is a complex variable with a real part of dr, // and an imaginary part of di matlab.GetFullMatrix("d", "base", ref dr, ref di); System.Array resultsR = new double[2, 2]; System.Array resultsI = new double[2, 2]; matlab.GetFullMatrix("ss", "base", ref resultsR, ref resultsI); Console.WriteLine(resultsR.GetValue(0, 0).ToString() + " " + resultsR.GetValue(0, 1).ToString() + " " + resultsR.GetValue(1, 0).ToString() + " " + resultsR.GetValue(1, 1).ToString()); }
static void Main(string[] args) { // Start the Configuration Manager //ConfigurationManager configManager = new ConfigurationManager(@"D:\Work\Research\PhD\Implementation\Diactrization\Preprocessing\Preprocessing\Configurations.xml"); ConfigurationManager configManager = new ConfigurationManager(args[0]); // Start the logger Logger logger = new Logger(configManager); // Start the train parser Parser trainParser; switch (configManager.trainPOSInputFormat) { case "RDI": trainParser = new RDIFormatParser(configManager, logger); break; case "Stanford": trainParser = new StanfordFormatParser(configManager, logger); break; default: trainParser = new RDIFormatParser(configManager, logger); break; } // Start Train Set parsing from root directory trainParser.Parse(configManager.rootTrainDirectory, "Train", configManager.trainPOSInputFormat); // Start the test parser Parser testParser; switch (configManager.testPOSInputFormat) { case "RDI": testParser = new RDIFormatParser(configManager, logger); break; case "Stanford": testParser = new StanfordFormatParser(configManager, logger); break; default: testParser = new RDIFormatParser(configManager, logger); break; } // Start Train Set parsing from root directory testParser.Parse(configManager.rootTestDirectory, "Test", configManager.testPOSInputFormat); // Copy files to configuration environment if required if (configManager.configEnvDirectory != "") { MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.Execute(@"load('" + configManager.rootTrainDirectory + @"\input_data');"); matlab.Execute(@"load('" + configManager.rootTestDirectory + @"\input_data');"); matlab.Execute(@"save('" + configManager.configEnvDirectory + @"\input_data');"); } }
static private double[] scores(DataTable kinectTable, double[] groundPlane, string exercise) { double[,] kinectData = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[,] kinectZeros = new double[kinectTable.Rows.Count, kinectTable.Columns.Count]; double[] groundPlaneZeros = new double[4]; System.Array cr = new double[3]; System.Array ci = new double[3]; for (int r = 0; r < kinectTable.Rows.Count; r++) { for (int c = 0; c < kinectTable.Columns.Count; c++) { kinectData[r, c] = (double)kinectTable.Rows[r][c]; } } MLApp.MLAppClass matlab = new MLApp.MLAppClass(); matlab.PutFullMatrix("CS_kinectData", "base", kinectData, kinectZeros); matlab.PutFullMatrix("CS_groundPlane", "base", groundPlane, groundPlaneZeros); String MyDocs = Environment.GetFolderPath(Environment.SpecialFolder.Personal); String ProjectLocation = "Visual Studio 2010\\Projects\\Kinect-Tracking-Project\\MatlabPrototypes\\FeatureDetection"; String matFileCD_command = String.Format("cd '{0}';", System.IO.Path.Combine(MyDocs, ProjectLocation)); matlab.Execute(matFileCD_command); if (exercise == "squats") { matlab.Execute("c = cs_matlab_classifier(CS_kinectData, CS_groundPlane, 'squats');"); } else if (exercise == "arm raise") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'arm raise'));"); } else if (exercise == "leg raise") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'leg raise'));"); } else if (exercise == "leg extension") { matlab.Execute("c = transpose(testBayes(CS_kinectData, CS_groundPlane, 'leg extension'));"); } matlab.GetFullMatrix("c", "base", cr, ci); double[] cr_d = new double[3]; cr_d = (double[])cr; return cr_d; }