Tcpserver and callbacks. How to pass variables?

3 次查看(过去 30 天)
Hello everyone,
i'm using the tcpserver function
t = tcpserver('127.0.0.1', 1234,"ConnectionChangedFcn",@connectionFcn)
So when a connection is estabilished the function connectionFcn is executed and it does some code.
I want to know if it is possible to pass some variables to this connectionFcn
For example i would like to pass this function an array, and within the connectionFcn stores some data that will be accessible for later computations.

回答(1 个)

Sharad
Sharad 2023-7-13
Hi,
As per my understanding, you are interested in knowing whether some variable can be passed to the connectionFcn while using tcpserver function.
It is possible to pass variables in the connectionFcn, and can be done in the following manner.
  • Declare and define the variable that you want to pass.
data = [1 2 3 4 5];
  • Call the tcpserver function in the following manner with parameters to connectionFcn.
t = tcpserver('127.0.0.1', 5678, "ConnectionChangedFcn", @(conn) connectionFcn(conn, myData));
  • Define the connectionFcn function.
function connectionFcn(conn, data)
% Your code here
% Use the 'data' variable as needed
disp(data);
end
Here are some documentation links that you might want to follow:
Thank you.
  2 个评论
Giovanni Di Girolamo
Thanks very much. I was looking at the anonymnous function and your example cleared my mind.
Ricardo Kehrle Miranda
I tryied your example and I get
Too many input arguments.
Error in tcpserver.internal.TCPServerCustomClient/connectionCallbackFunction
Any ideas?

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Interactive Control and Callbacks 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by