Data exchange between active Simulink model and Matlab script

13 次查看(过去 30 天)
Hello,
I need to establish a connection between a Simulink model and a GUI/Matlab script. Between those I want to transfer a big amount of data (arround 30.000 values so far).
This data is generated on an external system, which is communicating with the Simulink model via matrix transmitters and a TCP-IP connection. This connection already works. I have a problem with the exchange of data between Simulink and the GUI, because in order to work properly, the Simulink model has to run constantly. How can I transfer an array with all my calculated values to the GUI?
I already tried executing the Simulink model once from the script and transfer the data afterwards, but it takes way to long to connect with the matrix transmitter (like in the attached example).
The access over the runtime data of a block is also no solution, because I can only read one signal and not the whole value, right?
Is it possible to exchange this amount of data with a refresh rate around 1s or faster?
Attached is a simple example, instead of the TCP-IP connection it receives random values from the script and transfers them back (The TCP-IP block is commented out). The script just plots the random values in a plane.
I have to use MATLAB version R2017b.

回答(1 个)

Shubham
Shubham 2023-12-3
I understand that you want to establish a connection between a Simulink model and a MATLAB script. You want to transfer a large amount of data at a high refresh rate.
For establishing a tcp/ip connections please have a look at the following examples. These examples explain how to setup model parameters and setup a connection for transferring data:
I have tried the following at my end:
MATLAB script:
server = tcpserver('0.0.0.0',5000,ByteOrder="big-endian",ConnectionChangedFcn=@connectionFcn)
sim("simul.slx");
ans =[];
function connectionFcn(src, ~)
global ans;
while src.Connected
x = read(src,1,"double");
ans = [ans x];
pause(0.01); % to control refresh rate
if size(ans,2)==200
data=ans;
break;
end
end
disp(ans);
end
Simulink model:
The above MATLAB script reads a single value as “double” data type. You can read multiple values at a time after checking available bytes. You can also store the data in a datastore memory block in Simulink to store the data and access it at regular intervals.
I hope this helps!!
  1 个评论
Kolja Wehber
Kolja Wehber 2023-12-5
thanks for your reply. The problem is I have to use MATLAB version R2017b and the tcpserver function is not supported there.
Should have mentioned it in the original post though.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Sources 的更多信息

产品


版本

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by