Run within Matlab a written C# .exe program to obtain an output variable
Show older comments
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
More Answers (1)
tasneem
on 8 Mar 2024
0 votes
% 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))
Categories
Find more on Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!