How to define input parameters for BytesAvailableFcn (tcpclient)

Hy all,
my problem concerns the use of input arguments when I define the BytesAvailableFcn of a tcpclient connection.
I create a tcpclient connection:
t = tcpclient('address', port);
And I send a request to the server (which starts automatically to send data):
write( t, 'request' );
I want to have a callback function triggered by a bytes available event:
configureCallback( t, "terminator", @MyCallbackFcn );
MyCallbackFcn is written in a separated MyCallbackFcn.m file. The callback function MyCallbackFcn is triggeredd whenever a terminator is available to be read from the remote host specified by the TCP/IP client.
At the moment, MyCallbackFcn uses global parameters (var1, var2, etc.) defined in the main program:
function MyCallbackFcn( src, ~ )
global var1 var2
...
end
This algorithm works well. But in order to avoid the use of global parameters, I would like to define input parameters for MyCallbackFcn fonction. My problems start here, because I do not find the right form to write:
  • the definition of the callback function in the main program : configureCallback( t,...
  • the function MyCallbackFcn in the MyCallbackFcn.m file
Thank you in advance for your help.
Raphaël

 采纳的回答

6 个评论

Thanks for your answer Walter.
I read the Matlab documentation about "parameteriring functions" that you have indicated. This topic explains how to store or access extra parameters for mathematical functions that you pass to MATLAB® function functions, such as fzero or integral. But I want to use a function which is not a MATLAB function, so this solution seems to by not adapted to my problem.
configureCallback(t, @(src,event) MyCallbackFcn(src, event, var1, var2)
function MyCallbackFcn(stc, event, var1, var2)
Hi Walter,
thanks for the two lines of code, it works fine!
I knew it was possible to use input parameters with the configureCallback function, but I couldn't find the correct syntax...
In the syntax correct for the first statement?
configureCallback(t, @(src,event) MyCallbackFcn(src, event, var1, var2)
is correct syntax for configuring a callback to MyCallbackFcn passing in two extra parameters, including var1 and var2 . The var1 and var2 values would be "captured" at the time the @(src,event) was processed -- any changes to var1 or var2 made after that point in the code would have no effect on the configured callback.

We need to pass the "terminator" argument. Also, a closing parenthesis is missing.

configureCallback(t, "terminator", @(src, event) MyCallbackFcn(src, event, var1, var2))

Just small things but took me some time to figure out. Thanks.

请先登录,再进行评论。

更多回答(0 个)

产品

版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by