Exemplo n.º 1
0
        /// <summary>
        /// Fetch a variable from MATLAB into an 32-bit integer.
        /// </summary>
        /// <param name="variableName">Exact case-sensitive name in MATLAB.</param>
        /// <param name="clearAfterRetrieving">Indicates wether the variable should
        /// be deleted after the data has been retrieved.</param>
        /// <returns>32-bit integer</returns>
        public static int MatlabVariableToInteger(string variableName, bool clearAfterRetrieving)
        {
            object o = MatlabServer.GetVariable(variableName, "base");
            var    r = Convert.ToInt32(o);

            if (clearAfterRetrieving)
            {
                MatlabInterface.Execute("clear " + variableName);
            }
            return(r);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Initialize the COM Automation Server.
        /// </summary>
        public static void InitializeMatlab()
        {
            Type matlabType = Type.GetTypeFromProgID("Matlab.Desktop.Application");

            MatlabServer = (IMLApp)Activator.CreateInstance(matlabType);
            // set directory
            var dir = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\MATLAB";

            MatlabInterface.Execute(@"cd('" + dir + @"');");
            // delete all variables in memory and clear screen
            MatlabInterface.Execute(@"clear;clc;");
        }
Exemplo n.º 3
0
        /// <summary>
        /// Transfer an array of strings (holding data as integer, float, or double) to MATLAB - creating a vector.
        /// </summary>
        /// <param name="strArray"></param>
        /// <param name="nameInMatlab"></param>
        /// <param name="executeNow"></param>
        /// <returns></returns>
        public static string ArrayToMatlabVector(string[] strArray, string nameInMatlab, bool executeNow)
        {
            var command = new StringBuilder();

            command.Append(nameInMatlab + " = [");
            foreach (var str in strArray)
            {
                command.Append(str);
                command.Append(str != strArray.Last() ? " " : "];");
            }
            if (executeNow)
            {
                MatlabInterface.Execute(command.ToString());
            }
            return(command.ToString());
        }
Exemplo n.º 4
0
        /// <summary>
        /// Transfer an array of double-precision values into MATLAB.
        /// </summary>
        /// <param name="dblArray"></param>
        /// <param name="nameInMatlab"></param>
        /// <param name="executeNow"></param>
        /// <returns></returns>
        public static string ArrayToMatlabVector(double[] dblArray, string nameInMatlab, bool executeNow)
        {
            var command = new StringBuilder();

            command.Append(nameInMatlab + " = [");
            foreach (var i in dblArray)
            {
                var g    = Math.Round(i, 2);
                var gStr = g.ToString("F");
                command.Append(gStr);
                command.Append(i != dblArray.Last() ? " " : "];");
            }
            if (executeNow)
            {
                MatlabInterface.Execute(command.ToString());
            }
            return(command.ToString());
        }