MATLAB Answers

How to connect matlab and C# to create a winform gui to run a simulink model

20 views (last 30 days)
John Smith
John Smith on 16 Oct 2020
Answered: Mario Malic on 16 Oct 2020
I am new to Matlab and trying to code a winform based GUI for a simulink model in C#. But i had issue about how to communicate them efficiently.
2 different ways i tried:
1) I tried is COM communication between C# to matlab but after loading model, it was frozen.
C# script for communicating matlab over COM:
MLApp.MLApp matlab = new MLApp.MLApp();
matlab.Visible = 0;
matlab.Execute(@"cd('path to model')");
It looks like a buffer overflow to me but I couldn't find any resources on the subject.
How can i prevent freezing?
2) I tried to communicate c# and matlab to control simulink model using udp protocol. I coded a matlab script to be bridge. However, i realized that i need to create more than one thread that will communicate with each other. Eventhough i used parellel computing toolbox in matlab, i can not accomplish it.
matlab script :
u = udp('', 'LocalHost', '', 'LocalPort', 49013);
command = fread(u);
if(command == 1)
%start model
elseif(command == 3)
%pause model
else(command == 4)
%stop model
Is there a way to communicate threads each other in matlab?
Any suggestion would be nice.


Show 4 older comments
Mario Malic
Mario Malic on 16 Oct 2020
Try first loading simulink without window and then try to load the model, as it takes some time to start up.
What you can do as well is to leave the MATLAB window visible, and press control-c when it freezes so it will throw an error so you can know what the issue is.

Sign in to comment.

Accepted Answer

Mario Malic
Mario Malic on 16 Oct 2020
Based on the comments section, looks like this would be the way to start up the Simulink externally without freezing.
matlab.Execute("cd('path to model')");
% or
% matlab.Execute("open_system(model)");


Sign in to comment.

More Answers (0)


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!