简体   繁体   中英

Is it possible to use .NET application in MatLab?

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.

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 .

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. AC# 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);

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM