# Calling Julia from EMTDC/PSCAD or MATLAB code

**URL:** <https://discourse.julialang.org/t/calling-julia-from-emtdc-pscad-or-matlab-code/52417>\
**Category:** Modelling & Simulations\
**Tags:** question, package\
**Created:** [December 26, 2020, 7:22pm UTC](https://discourse.julialang.org/t/calling-julia-from-emtdc-pscad-or-matlab-code/52417 "2020-12-26T19:22:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![anchasatish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/anchasatish/32/20094_2.png) [@anchasatish](https://discourse.julialang.org/u/anchasatish)\
**Post date:** [December 26, 2020, 7:22pm UTC](https://discourse.julialang.org/t/calling-julia-from-emtdc-pscad-or-matlab-code/52417/1 "2020-12-26T19:22:05Z")

</div>

I would like to use Julia programming as a software in loop for my EMTDC/PSCAD or MATLAB simulation i.e., optimization problem solved in julia and output of this optimization solution is given to the Simulation platform and again output of this simulation is given as input to the julia for optimization.  
As I found some julia modules like PowerModels.jl for application of power systems optimization, I want to use same for a software in loop for my research work. If we can use julia as a software in loop, please let me know how to do it.

---

<div class="post-metadata">

**Author:** ![MA\_Laforge](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ma_laforge/32/385_2.png) [@MA\_Laforge](https://discourse.julialang.org/u/MA_Laforge)\
**Post date:** [December 27, 2020, 10:11pm UTC](https://discourse.julialang.org/t/calling-julia-from-emtdc-pscad-or-matlab-code/52417/2 "2020-12-27T22:11:18Z")

</div>

I would start looking at basic **socket communication** :

> **[Sockets · The Julia Language](https://docs.julialang.org/en/v1/stdlib/Sockets/)**
>
> Documentation for The Julia Language.

* * *

[https://www.mathworks.com/help/instrument/communicate-using-tcpip-server-sockets.html](https://www.mathworks.com/help/instrument/communicate-using-tcpip-server-sockets.html)

* * *

### How do I use sockets?

In most environments, you can read & write (commands) much the same way as you would a file (once the socket is open).

### Problem with sockets

The problem with socket is that they are relatively low level. You might have to define your own communication protocol (example):

- Matlab-\>Julia: “Run operation X in Julia”
- Julia-\>Matlab: “Ok, I’m done with operation X” (i.e. some form of ACK)
- etc.

### Alternatives

There are higher level tools out there that can make things simpler:

- RPC: remote procedure calls
- (Sorry, I don’t know that many of them)

Simulink seems to support other protocols, listed here:  
 → [https://www.mathworks.com/help/slrealtime/communication-protocols.html](https://www.mathworks.com/help/slrealtime/communication-protocols.html)

But I’m not sure which ones would be practical for this application (probably not CAN or RS-232 in your case).

I know many CAD tools support the LXI protocol (typically through ethernet) to talk to instruments that perform measurements. There might be a way to use that somehow. But again, I think **socket communication** is probably a better starting point.

### What if my application doesn’t support sockets?

Linux systems provide the `mkfifo` command to create “named pipes”. It creates a “file” that pretty much any program can write to in order to communicate with your Julia socket:  
 → [Using Named Pipes (FIFOs) with Bash | Linux Journal](https://www.linuxjournal.com/content/using-named-pipes-fifos-bash)

---

<div class="post-metadata">

**Author:** ![Orbots](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orbots/32/3392_2.png) [@Orbots](https://discourse.julialang.org/u/Orbots)\
**Post date:** [December 31, 2020, 12:03am UTC](https://discourse.julialang.org/t/calling-julia-from-emtdc-pscad-or-matlab-code/52417/3 "2020-12-31T00:03:46Z")

</div>

I use ZMQ.jl and MessagePack.jl to pack up dictionaries of data and send them over a socket to julia and then back. This means you need zeromq and messagepack in your EMTDC/PSCAD or MATLAB code too. It’s actually a pretty nice way to work.

Alternatively you can try to embed julia in MATLAB etc, but this has it’s own problems. I’m not embedding in MATLAB, so maybe you can get it working there, but I’ve essentially given up on this route for my own plugin. I check back on PackageCompiler.jl every few months to see if there is any progress on this front, but it’s kinda gone backwards recently. Some info in the docs too: [Embedding Julia · The Julia Language](https://docs.julialang.org/en/v1/manual/embedding/)

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [July 20, 2021, 11:46am UTC](https://discourse.julialang.org/t/calling-julia-from-emtdc-pscad-or-matlab-code/52417/4 "2021-07-20T11:46:34Z")

</div>

Hi @Orbots, would you like to share some details how you got the calling-Julia-from-Matlab working?

---

<div class="post-metadata">

**Author:** ![Orbots](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/orbots/32/3392_2.png) [@Orbots](https://discourse.julialang.org/u/Orbots)\
**Post date:** [July 20, 2021, 5:12pm UTC](https://discourse.julialang.org/t/calling-julia-from-emtdc-pscad-or-matlab-code/52417/5 "2021-07-20T17:12:36Z")

</div>

I don’t use this from Matlab. I do use it from a number of other tools. Basically anywhere I can write C++ plugins.

From Julia:  
use the packages

> **[GitHub - JuliaInterop/ZMQ.jl: Julia interface to ZMQ](https://github.com/JuliaInterop/ZMQ.jl)**
>
> Julia interface to ZMQ. Contribute to JuliaInterop/ZMQ.jl development by creating an account on GitHub.

> **[GitHub - JuliaIO/MsgPack.jl: Julia MsgPack implementation with type-driven,...](https://github.com/JuliaIO/MsgPack.jl)**
>
> Julia MsgPack implementation with type-driven, overloadable packing/unpacking functionality - GitHub - JuliaIO/MsgPack.jl: Julia MsgPack implementation with type-driven, overloadable packing/unpack...

From Matlab:

> **[GitHub - smcgill3/zeromq-matlab: ZeroMQ mex bindings for MATLAB](https://github.com/smcgill3/zeromq-matlab)**
>
> ZeroMQ mex bindings for MATLAB. Contribute to smcgill3/zeromq-matlab development by creating an account on GitHub.

> **[GitHub - bastibe/matlab-msgpack: msgpack.org\[Matlab\]](https://github.com/bastibe/matlab-msgpack)**
>
> msgpack.org\[Matlab\]. Contribute to bastibe/matlab-msgpack development by creating an account on GitHub.

Run Matlab.  
Run you Julia program in a terminal or notebook.

Choose a local port address to use. e.g. “tcp://\*:5556”  
You need to write a little loop in julia or matlab ( depending on which is the “server” ) using zeromq to listen on that address for the packets, unpack and dispatch on your work function. When the work function is done you return the result via zeromq. From the other end you pack up your data and send via zeromq to the address.

Packing and unpacking is via msgpack, so you can use any data supported by msgpack. I use msgpack ( rather than JSON ) because it has the option of extending to use your own types.

Hope this helps.

---

<div class="post-metadata">

**Author:** ![Bardo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bardo/32/21601_2.png) [@Bardo](https://discourse.julialang.org/u/Bardo)\
**Post date:** [July 20, 2021, 9:52pm UTC](https://discourse.julialang.org/t/calling-julia-from-emtdc-pscad-or-matlab-code/52417/6 "2021-07-20T21:52:20Z")

</div>

Thanks a lot!
