Run within Matlab a written C# .exe program to obtain an output variable
10 ビュー (過去 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?
0 件のコメント
採用された回答
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
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).
その他の回答 (1 件)
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))
0 件のコメント
参考
カテゴリ
Help Center および File Exchange で Call MATLAB from .NET についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!