Matlab - Grasshopper Interaction (Advanced)

I need to pass data back and forth repeatedly between Grasshopper and MATLAB.

In this post, Mostapha showed one way of doing this. This works, however the method seems a bit slow and prone to errors since it relies on writing and deleting a file, using pause() functions and a Grasshopper timer. He does mention:

Note: Obviously this method is not the most sophisticated method that you can use but it is simple enough to set-up easily and fast.

So my question is: What is a more sophisticated way of doing this?

All the best,
Max

@MaxMarschall The post you mention is 6 years ago. Could you tell what are going to do for Matlab - Grasshopper Interaction .If only for visualization and basic statics, Grasshooper is enough for you.

Hi @minggangyin,

It has to do with some dynamic energy simulations that I am running.

The setup was a bit too complicated to explain here, but in essence: I need to create a loop where I compute something in Grasshopper (or GHPython), send the results to Matlab, compute something in Matlab based on that data, then send results back to Grasshopper and so on.

The tricky thing here seems to be the timing. I’d like Grasshopper and Matlab to each “know” when the other has finished its operation, and only then immediately do their own operation. Is that even possible?

It should be doable in principle. The key here will be using the timer component in grasshopper.

  1. Do the process in gh to register data somewhere (preferably excel)
  2. Observe the time for the first step to get finished.
  3. Setup a timer component for time greater than or equal to step 02
  4. Read the data back into grasshopper once it has been processed with Matlab
    test.xlsx (7.7 KB)
    unnamed.gh (20.0 KB)

Is it possible to compile your Matlab code and run it as an executable? That can make everything much cleaner as you can run Matlab as a subprocess and wait for it to be finished.

Thanks for your answers. In my case the subprocess idea might work. I’ll give that a shot and post back with the solution if I succeed.

As I know, you should first install the matlab.engine API for python, then use the GHCpython component(https://www.food4rhino.com/app/ghcpython) to link the two.

2 Likes

Hello @MaxMarschall

I’m trying to do the same. I already have a long script in Matlab (actually was my professor Ph.D.) and I need to call this on my gh workflow and retrieve its result, this has to be automated since I am performing an optimization.
Could you already solve this?

Thanks for your help.
Sebastian

hello @minggangyin

I wanted to make connection between grasshopper and MATLAB for optimization. Would you pleas tell me how it is possible?

As I know, it is very hard to do it. Maybe you can use python in Grasshopper to connect Matlab.
https://python-control.readthedocs.io/en/0.8.3/generated/control.matlab.bode.html

I really appreciate your valuable response.

Hi
I am working on a research that I need to find a way to make a connection between grasshopper and Matlab.

Have you ever find a solution for your problem?