๐Ÿ–ฅ๏ธ Coding/MATLAB & Simulink

MATLAB , C# .NET DLL ๊ฐ„ ์‚ฌ์šฉ๋ฐฉ๋ฒ•

SpaceJ 2021. 7. 26. 17:31
๋ฐ˜์‘ํ˜•

MATLAB์—์„œ C#์„ ํ˜ธ์ถœํ•˜๊ฑฐ๋‚˜ C#์—์„œ MATLAB ๊ธฐ๋Šฅ์„ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค. (MATLAB ๋ฌธ์„œ)

using System;
namespace netdoc
{
    public class SampleRefTest
    {
        //test ref keyword
        public void refTest(ref double db1)
        {
            db1 = db1 * 2;
        }
    }
}

MATLAB์—์„œ ์œ„ C# ํด๋ž˜์Šค๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

asmpath = 'c:\work\Visual Studio 2012\Projects\SampleRefTest\SampleRefTest\bin\Debug\';
asmname = 'SampleRefTest.dll';
asm = NET.addAssembly(fullfile(asmpath,asmname));
cls = netdoc.SampleRefTest;
db4 = refTest(cls,6);
// db4 = 12

๋ฐ˜๋Œ€๋กœ C#์—์„œ MATLAB์œผ๋กœ ์ž‘์„ฑ๋œ ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

using System; 
using System.Collections.Generic; 
using System.Text; 

namespace ConsoleApplication2 
{ 
    class Program 
    { 
        static void Main(string[] args) 
        { 
            // Create the MATLAB instance 
            MLApp.MLApp matlab = new MLApp.MLApp(); 

            // Change to the directory where the function is located 
            matlab.Execute(@"cd c:\temp\example"); 

            // Define the output 
            object result = null; 

            // Call the MATLAB function myfunc
            matlab.Feval("myfunc", 2, out result, 3.14, 42.0, "world"); 
             
            // Display result 
            object[] res = result as object[]; 
             
            Console.WriteLine(res[0]); 
            Console.WriteLine(res[1]); 
            // Get user input to terminate program
            Console.ReadLine();
        } 
    } 
}

๋งคํŠธ๋žฉ ์Šคํฌ๋ฆฝํŠธ

๋ฐ˜์‘ํ˜•