필터 지우기
필터 지우기

Run within Matlab a written C# .exe program to obtain an output variable

조회 수: 14 (최근 30일)
Hi Matlab community,
I would like to be able to run an .exe code (created in C#) and then get the output into a Matlab variable. This would allow to fasten some tedious calculations.
See below the C# code that I've written used for testing. It is just a function that return a the input number after squaring it. I obtain the TestMatlab.exe file (See the file attached).
using System;
namespace TestMatlab
{
class Program
{
static int f(int x)
{
int returnvalue = x * x;
return returnvalue;
}
static void Main(string[] args)
{
f(Convert.ToInt32(args[0]));
Console.WriteLine(f(3));
}
}
}
And this is how I run the .exe in Matlab afterwards :
% Just define some of the space and bracket needed otherwise it get visually hard to understand
bracket = '"'; space=' ';
% Run the code - input number is 3 so expect 9 as an output
% can use "system" or "dos"
system(['C\Users\MatlabCode\' 'TestMatlab.exe', space ,bracket, num2str(3),bracket]);
Output is the following:
>> Results = system(['C\Users\MatlabCode\' 'TestMatlab.exe', space ,bracket, num2str(3),bracket]);
9
>> Results
Results =
0
The results "9" is just written there and it is not assigned to any variable. Is there a workaround this?

채택된 답변

Kojiro Saito
Kojiro Saito 2018년 9월 14일
By assigning two output variables, results of Console.WriteLine can be passed to the variable. In the following example, 9 will be caught in cmdout variable as a character.
[status, cmdout] = system(['CsharpFromMatlab.exe', space ,bracket, num2str(3),bracket]);
% In this case, cmdout will be '9\n'
% Eleminate a line change character and convert characters to double
Results = str2double(sscanf(cmdout, '%s\n'));
  댓글 수: 3
Kojiro Saito
Kojiro Saito 2018년 9월 18일
One way is create .NET dll and call it from MATLAB. Ref: this document (Call .NET Methods with Optional Arguments)
Create TestClass.dll from the following C# code.
TestClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace dotNetTestClass
{
public class TestClass
{
// Add
public double TestAdd(double a, double b)
{
return a + b;
}
}
}
Then, call this dll from MATLAB
% Make dll visible to MATLAB
asm = NET.addAssembly('C:\hoge\dotNetTestClass.dll');
% Make the dll available
cls = dotNetTestClass.TestClass;
a = 3.14;
b = 100;
% Call the dll function
addcalc = TestAdd(cls, a, b);
We can get C# dll result and assign it to MATLAB variable (addcalc, in this case).
vincent caillet
vincent caillet 2018년 9월 18일
That's absolutely perfect, thanks for that! I found so many threads online with people desperate to make this work. This is going to be so helpful.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

tasneem
tasneem 2024년 3월 8일
% Define region of interest
latlims = [39.5 40];
lonlims = [-105.6 -105.1];
% Define grid of target locations in region of interest
tgtlatv = linspace(latlims(1),latlims(2),50);
tgtlonv = linspace(lonlims(1),lonlims(2),50);
[tgtlons,tgtlats] = meshgrid(tgtlonv,tgtlatv);
tgtlons = tgtlons(:);
tgtlats = tgtlats(:);
Z = elevation(txsite("Latitude",tgtlats,"Longitude",tgtlons));
[Zmin, Zmax] = bounds(Z);
Zmean = mean(Z);
disp("Ground elevation (meters): Min Max Mean" + newline + ...
" " + round(Zmin) + " " + round(Zmax) + " " + round(Zmean))

카테고리

Help CenterFile Exchange에서 .NET Client Programming에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by