Tcpserver and callbacks. How to pass variables?

조회 수: 3 (최근 30일)
Giovanni Di Girolamo
Giovanni Di Girolamo 2023년 7월 13일
댓글: Ricardo Kehrle Miranda 2023년 7월 19일
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
Giovanni Di Girolamo 2023년 7월 13일
Thanks very much. I was looking at the anonymnous function and your example cleared my mind.
Ricardo Kehrle Miranda
Ricardo Kehrle Miranda 2023년 7월 19일
I tryied your example and I get
Too many input arguments.
Error in tcpserver.internal.TCPServerCustomClient/connectionCallbackFunction
Any ideas?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Animation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by