![matlab return matlab return](https://it.mathworks.com/help/matlab/ref/datacursormode_coordinates.png)
So with weak reference code for that will look like this: To destroy COM instance, we can use method Marshal.FinalReleaseComObject. Tracking should be employed through WeakReference class, so whenever Garbage Collector would want to destroy Matlab instance, it should not be stopped by our tracking class.
![matlab return matlab return](https://i.stack.imgur.com/FwH9R.png)
It should track one of the created instances and in case anything bad happens (but dynamic variable cast is probability ), it will close all instances prior to application exit. To that, I recommend creating some special class that will create an instance or instances of Matlab. But still, it is a huge memory leak risk if not maintained properly. If not, it will close nicely with console window. If you terminate it by disabling debugging or it will close due to error, Matlab window will remain opened. If you executed any of these examples, you probably took notice of a very simple Matlab window which had opened during the life of your console app. How to execute commands and check them for errors. Now we know how to put and get data from Matlab. Strings are much more friendly and there are just string. But I guess it is just what you will like better. And since object you have to create first and then cast it also, don't bother and just use dynamic. Matlab.GetWorkspaceData( " d", " base", out e) The third parameter is the value of our variable. In the end, I created in my application another method that encapsulated PutWorkspaceData and forgot about it. If I remember it right, I found it in some code example and it should be only " base". It is not mentioned in the documentation of this method. But it is called? This one took much more time than I would want. To set variable correctly (so you could reference it in command), you must use global workspace.
![matlab return matlab return](https://www.picclickimg.com/d/l400/pict/274789468940_/Computer-Explorations-in-Signals-and-Systems-Using-MATLAB.jpg)
First is the desired name of our new Matlab variable. To send some parameters along with our command, we have to use one of ' Put*' methods. So to check our command had errors, we have to check if output string has " ? Error" in it. Let's try to run something like this: ' 1*', which will result in error: It will have ' error' string in response. Also, there is one way to find out if our statement had errors.
![matlab return matlab return](https://i.stack.imgur.com/G4TAT.png)
This way, we can send and receive string messages with MATLAB only. It's really simple and more importantly, it works! It should return also a simple string in console: In more complex applications or for web applications or other programs which run for a long time, it may create big memory leaks since 1 instance costs around 220 Mb (on 64 bit Windows 7 and Matlab R2010a).Īfter creating Matlab program, execute a simple sum of 2 integers, just for testing communication - we don't need anything more sophisticated. Contrary to that, we could use " matlab.application" which will create another instance of Matlab anytime Activator.CreateIntance method will be executed. When it will try to create another Matlab, it will just return a reference to the same object. Program Id " " means single Matlab COM server for our application. This code will create a single Matlab COM server through Activator class. Var matlab = (MLApp.MLApp)Activator.CreateInstance(activationContext) Ĭonsole.WriteLine(matlab.Execute( " 1+2")) To do that, we can add code to our main program:Ĭopy Code var activationContext = Type.GetTypeFromProgID( " ") In order to use it, we should create our Matlab server from C# application. You should get a new reference like below: Then choose "Matlab Application (Version 7.10) Type Library". First, we will add DLL reference with COM interface. Let's start with a simple console application. it was only a test so there might be other problems that I am not aware of. I also tried with 7.1 and (if I remember correctly) it required only to swap reference in Visual Studio. Since interface for communicating with Matlab server is dependent on installed Matlab version and registry entries, it may have been different from yours. But I had only this version at my disposal. Important: I was using R2010a version of Matlab. Nonetheless, I accomplished why I was required to do, so I decided to share this with the rest of the world. I guess writing programs that use Matlab for calculating is not encouraged by MathWorks, you would became competition that way. I have found it hard to use it mainly due to lack of documentation, which is really basic with only few code examples for C#. While writing an application for my Masters degree diploma, I wrote a simple application that was using COM Matlab server.