# Matlab - Grasshopper Interaction (Advanced)

**URL:** <https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642>\
**Category:** grasshopper\
**Created:** [July 1, 2019, 6:48am UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642 "2019-07-01T06:48:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaxMarschall](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/maxmarschall/32/393_2.png) [@MaxMarschall](https://discourse.ladybug.tools/u/MaxMarschall)\
**Post date:** [July 1, 2019, 6:48am UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/1 "2019-07-01T06:48:57Z")

</div>

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

In [this post](https://www.grasshopper3d.com/profiles/blog/show?id=2985220%3ABlogPost%3A759485&page=1#comments), 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

---

<div class="post-metadata">

**Author:** ![minggangyin](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/minggangyin/32/216_2.png) [@minggangyin](https://discourse.ladybug.tools/u/minggangyin)\
**Post date:** [July 1, 2019, 7:48am UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/2 "2019-07-01T07:48:18Z")

</div>

@MaxMarschall The post you mention is 6 years ago. Could you tell what are going to do for [Matlab - Grasshopper Interaction](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642) .If only for visualization and basic statics, Grasshooper is enough for you.

---

<div class="post-metadata">

**Author:** ![MaxMarschall](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/maxmarschall/32/393_2.png) [@MaxMarschall](https://discourse.ladybug.tools/u/MaxMarschall)\
**Post date:** [July 1, 2019, 8:33am UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/3 "2019-07-01T08:33:51Z")

</div>

Hi @minggangyin,

It has to do with some [dynamic energy simulations](https://discourse.ladybug.tools/t/agent-based-building-simulation/6563) 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?

---

<div class="post-metadata">

**Author:** ![devang](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/devang/32/21620_2.png) [@devang](https://discourse.ladybug.tools/u/devang)\
**Post date:** [July 1, 2019, 8:52am UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/4 "2019-07-01T08:52:39Z")

</div>

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](https://discourse.ladybug.tools/uploads/default/original/2X/5/5b88cdb434e3ec8a3a7868e9005be8a0fb1cf7c5.xlsx) (7.7 KB)  
 ![timer](https://discourse.ladybug.tools/uploads/default/original/2X/e/eaa9bdd1710bfa2ca09ad605a714a602852a3090.png) [unnamed.gh](https://discourse.ladybug.tools/uploads/default/original/2X/2/2f2e8c8cbaf6256d35e10cb37b110c3b4ae9ce14.gh) (20.0 KB)

---

<div class="post-metadata">

**Author:** ![mostapha](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/mostapha/32/15244_2.png) [@mostapha](https://discourse.ladybug.tools/u/mostapha)\
**Post date:** [July 1, 2019, 1:35pm UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/5 "2019-07-01T13:35:15Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![MaxMarschall](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/maxmarschall/32/393_2.png) [@MaxMarschall](https://discourse.ladybug.tools/u/MaxMarschall)\
**Post date:** [July 1, 2019, 2:10pm UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/6 "2019-07-01T14:10:37Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![miraclema](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/miraclema/32/11597_2.png) [@miraclema](https://discourse.ladybug.tools/u/miraclema)\
**Post date:** [August 15, 2019, 5:35pm UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/7 "2019-08-15T17:35:09Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Seguto](https://discourse.ladybug.tools/letter_avatar_proxy/v4/letter/s/85e7bf/32.png) [@Seguto](https://discourse.ladybug.tools/u/Seguto)\
**Post date:** [July 13, 2020, 6:19pm UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/8 "2020-07-13T18:19:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![baharvojdani](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/baharvojdani/32/19443_2.png) [@baharvojdani](https://discourse.ladybug.tools/u/baharvojdani)\
**Post date:** [December 3, 2021, 6:33am UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/9 "2021-12-03T06:33:09Z")

</div>

hello @minggangyin

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

---

<div class="post-metadata">

**Author:** ![minggangyin](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/minggangyin/32/216_2.png) [@minggangyin](https://discourse.ladybug.tools/u/minggangyin)\
**Post date:** [December 3, 2021, 6:38am UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/10 "2021-12-03T06:38:36Z")

</div>

> [@baharvojdani](#):
>
> 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](https://python-control.readthedocs.io/en/0.8.3/generated/control.matlab.bode.html)

---

<div class="post-metadata">

**Author:** ![baharvojdani](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/baharvojdani/32/19443_2.png) [@baharvojdani](https://discourse.ladybug.tools/u/baharvojdani)\
**Post date:** [December 3, 2021, 6:45am UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/11 "2021-12-03T06:45:53Z")

</div>

I really appreciate your valuable response.

---

<div class="post-metadata">

**Author:** ![baharvojdani](https://discourse.ladybug.tools/user_avatar/discourse.ladybug.tools/baharvojdani/32/19443_2.png) [@baharvojdani](https://discourse.ladybug.tools/u/baharvojdani)\
**Post date:** [January 4, 2022, 8:00am UTC](https://discourse.ladybug.tools/t/matlab-grasshopper-interaction-advanced/6642/12 "2022-01-04T08:00:13Z")

</div>

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?
