Tcpserver and callbacks. How to pass variables?

4 views (last 30 days)
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.

Answers (1)

Sharad
Sharad on 13 Jul 2023
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 Comments
Giovanni Di Girolamo
Giovanni Di Girolamo on 13 Jul 2023
Thanks very much. I was looking at the anonymnous function and your example cleared my mind.
Ricardo Kehrle Miranda
Ricardo Kehrle Miranda on 19 Jul 2023
I tryied your example and I get
Too many input arguments.
Error in tcpserver.internal.TCPServerCustomClient/connectionCallbackFunction
Any ideas?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!