Reputation:
I feel at home in C# and I'll design a console application that will fetch some data from a web service. I'd like to allow a group of scientists to use the data in MatLab. One way to achieve that is to store it in a file on the HDD and then load the contents of it into MatLab.
That got me thinking, though. Is it possible to create a function in MatLab that calls an EXE created in C# so my scientific friends can go:
a = GetMeSomeData()
and populate the variable a with the response of the service? (I would, of course, format the data using my C# code so it'll fit the matrix model of MatLab.)
I've done some googling before I start working on this but most of the stuff I've found is about an old version of MatLab (2007) and it's said something about creating a COM object.
Upvotes: 8
Views: 6816
Reputation: 10708
You can easily call functions in a .NET assembly. First you have to tell Matlab what assembly you are going to use:
NET.addAssembly("path//to//assembly.dll");
After that, you just call functions in your m-files:
foo = Namespace.Class.FunctionFoo(input1, input2);
bar = Namespace.Class.FunctionBar(input1, input2);
You can even have multiple outputs. A C# function like this:
public void MultipleOut(int in1, int in2, out int out1, out int out2)
{ ... }
can be called like this in Matlab:
[out1, out2] = Namespace.Class.MultipleOut(in1, in2);
Upvotes: 3
Reputation: 612844
To answer the question directly, you can use the system
command or !
in MATLAB to execute an external process. Once it returns you can read and parse the output from your MATLAB function. Wrap that all up in a MATLAB .m file and you have what you describe in the question.
Of course, you could just access the web service directly from MATLAB with createClassFromWsdl
. And as others point out, NET.addAssembly
allows you to import and use your .net assembly directly from MATLAB, which may be cleaner than parsing text file output.
Upvotes: 2
Reputation: 4516
You might want to take a look at this: Using .NET libraries in MATLAB.
There's plenty of documentation there.
Start with NET.addAssembly
('path-to-dll)
, and then it seems you can just use the classes normally as you would in .NET.
You might also want to take a look at Using Arrays with .NET Applications.
Upvotes: 5