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

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')");
matlab.Execute("model");
matlab.Execute("set_param('model','SimulationCommand','start')");
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', '127.0.0.1', 'LocalPort', 49013);
fopen(u);
while(true)
command = fread(u);
if(command == 1)
%start model
set_param('model','SimulationCommand','start')
elseif(command == 3)
%pause model
set_param('model','SimulationCommand','pause')
.
.
.
else(command == 4)
%stop model
set_param('model','SimulationCommand','pause')
end
end
Is there a way to communicate threads each other in matlab?
Any suggestion would be nice.

7 comentarios

Matlab_Inst = actxserver('matlab.application')
Matlab_Inst.Execute("open_system('sldemo_autotrans')")
Don't know what's the purpose of @ in your cd command.
These two lines open up simulink for me.
John Smith
John Smith el 16 de Oct. de 2020
Editada: John Smith el 16 de Oct. de 2020
I think there is confusion because the code you shared is for matlab but in my question I tried to communicate with C# and matlab over COM. I edited question to clarify it. Thank you for your answer.
I will try to implement your suggestion to my matlab script that i designed as bridge between simulink and C#.
Thanks again.
Btw, @ is a 3 notation for string :
https://stackoverflow.com/questions/556133/whats-the-in-front-of-a-string-in-c
That's correct, so this would be the change in your C# setup.
matlab.Execute("cd('path to model')");
matlab.Execute("model");
% or
% matlab.Execute("open_system(model)");
I tried but it continues to freeze.
Thanks again.
Try first loading simulink without window and then try to load the model, as it takes some time to start up.
start_simulink
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.
It worked.
Thank you :D
Ok, then I'll post my comments as an answer so you can close this question.

Iniciar sesión para comentar.

 Respuesta aceptada

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')");
matlab.Execute("start_simulink")
matlab.Execute("model");
% or
% matlab.Execute("open_system(model)");

Más respuestas (0)

Categorías

Más información sobre Configure and View Diagnostics en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 16 de Oct. de 2020

Respondida:

el 16 de Oct. de 2020

Community Treasure Hunt

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

Start Hunting!

Translated by