Matlab in GH

Hello everyone,

I need to run a Matlab script from GH and retrieve the results, and this needs to be automated for optimization purposes.

I have been doing research and found out that is possible to run Matlab with python (https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html) and use external python packages with this component in grasshopper (https://www.food4rhino.com/app/gh-python-remote). Now, I can use for instance NumPy and other python packages.

However, Matlab.engine is not working yet.

Do you guys know how we can achieve this? Any ideas or suggestions?

1 Like

Hi,

we have written a small .net middleware hosted in Matlab, basically a dotnet object which has the purpose to do interprocess communication with other .net apps. Not so easy to create, but extremly powerful, stable and efficent.

Please note, in Rhino you use IronPython which is .Net based and not CPython. Numpy won’t work.
But you can also create net objects in Matlab! All you need to do is to create bidirectional listeners.

A much simpler, but limited way is exchanging .mat files (you’ll find packages of the older mat format on nuget or npm). In order to observe if a file has changed or a new one had been created you could create a FileSystemWatcher in both instances.

2 Likes

Hello @TomTom
Thank you for your reply
I have some experience with Python and Matlab, however, what you have mentioned it’s quite complicated for me.
Do you have any examples or a link that I can follow?

Unfortunally I’m not allowed to share any of this. Nor is the first approach easy or documented at all. Its rather more about general software development and a lot of trial and error. Writing matfiles however isn’t difficult at all, there are some 3rd party libraries just as I said. You could also choose any other format, it will just require to read and write it in both applications. The FileSystemWatcher is not complicated. You can invoke it with IronPython.

https://docs.microsoft.com/de-de/dotnet/api/system.io.filesystemwatcher?view=netcore-3.1.

Reading and writing files shouldn’t be so difficult, monitoring it either… I’m sure there is an similar functionality in CPython.

If you choose the Interprocess communication you can use Shared Memory or Pipes or local Tcp / Udp …

There are dozens of options.

There are ways to get CPython (and numpy) inside .net application. It requires Windows10 (it fails on Win7 and I haven’t tested Mac)

I’m pretty sure you can do alot of crazy stuff, but I found out that if you reduce the amount of unusual workarounds, the more fun it makes to program something. I was just giving feedback on how we interact with Matlab with our XIL/ECU Testing software. I’m pretty sure you can do a lot of this similar with CPython. We don’t need any math library because the simulation is done in Matlab. I believe the key here is using a middleware doing interprocess communication, which is great because you decouple two applications, if one crashes the other one keeps alive. I’m sure numpy is great, but only a few using because of things they couldn’t do with standard language features. And there is also always a equal library for .net apps (Math(.)Net) .

Is this com objects wrapper or some c/cli to c# wrapper?

Yeah, no one can argue with that. I am clearing things out so he has a more complete knowledge and can make a more informed unbiased decision.

Also the library I mention is not much different from your solution, as it is not using an integrated (in dotnet) python interpreter but a local CPython installation. Seems similar to using Matlab through dotnet middleware.

Interprocess communication enables sharing data between apps running in different processes. A com server is such thing, but its often a total overkill if you ask met. The most basic ipc of course is a file. Usally whats meant by this is using Shared Memory, Anonymous or Named pipes, sockets -> local tcp or udp optionally with protocols such as grpc, signals message queues, memory mapped files etc.
So there are many options.

1 Like

I previously wrote my information to a text file and then used subprocess to call matlab along with the command file to run. Then the matlab would output to another text file which I read the results from. Unfortunately I don’t think it is suited for your needs as it opens and closes a new instance of matlab each time which was quite a significant time sink.