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

10 views (last 30 days)
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?

Accepted Answer

Kojiro Saito
Kojiro Saito on 14 Sep 2018
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 Comments
Kojiro Saito
Kojiro Saito on 18 Sep 2018
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 on 18 Sep 2018
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.

Sign in to comment.

More Answers (1)

tasneem
tasneem on 8 Mar 2024
% 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))

Community Treasure Hunt

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

Start Hunting!