Matlab socket is not interruptible?

20 visualizaciones (últimos 30 días)
Yu-Chih Tung
Yu-Chih Tung el 29 de Mzo. de 2017
Comentada: Yu-Chih Tung el 11 de Abr. de 2017
I am developing a Matlab UI/Socket program where users can click a button to start and read a tcp socket. It is obviously that such a design need the tcp socket not block the UI thread. I mainly reference a Matlab UI interruption guideline to build my program. Setting the 'Interruptible' flag to 'on' does achieve my expected behavior.
However, when I add the socket code into this "interruptible" callback, everything stop working. Here is the example code:
function create_update_waitbar
h_wait = waitbar(0,'Please wait...',...
port = 50005;
socket=tcpip('', port, 'NetworkRole', 'server', 'Timeout', 60*60*24);
fprintf(2, '=== start wait connection to port = %d ===\n', port);
fprintf(2, '=== succeesfully get a connection to port = %d ===\n', port);
for i=1:10000,
if ishandle(h_wait)
action=fread(socket, 1, 'int8')
fprintf('\nwait: %.1f: ', (i/10000)*100);
% When waitbar reaches max, close it.
if ishandle(h_wait)
I basically just add a socket listening in the begin of the original callback function from the Matlab sample code and add a fread of that socket inside the waiting bar updating loop.
I expect the program can still let me use the UI (e.g., click the button to draw a figure as shown in the Matlab interruptible UI guideline), but it doesn't. This code will hang forever on the socket listening and socket read operations.
Any suggestion of achieving the goal of UI to control a Matlab socket?

Respuesta aceptada

Prannay Jain
Prannay Jain el 11 de Abr. de 2017
Currently in MATLAB, when you try to open a connection (either as the 'server' or the 'client') using a 'tcpip' object, the call blocks MATLAB execution until either the connection is made or the call times out. In short, MATLAB tcpip socket is not interruptible.
I work for MathWorks and have provided this feedback to the developers.
  1 comentario
Yu-Chih Tung
Yu-Chih Tung el 11 de Abr. de 2017
Thank you for confirming it. It is exactly what I expected. I have build a customized Java class to achieve the function I want.

Iniciar sesión para comentar.

Más respuestas (2)

Prannay Jain
Prannay Jain el 6 de Abr. de 2017
'tcpip' is not a base MATLAB function. It comes with Instrument Control Toolbox. Can you try with 'tcpclient' which comes with base MATLAB to create tcp/ip socket?
  1 comentario
Yu-Chih Tung
Yu-Chih Tung el 6 de Abr. de 2017
Hi Prannay,
Just wanna let you know that I need the Matlab to be the server (not client). But your reply seems also useful to me in the future. I have built my customized Java server class (interfaced to Matlab) that can achieve listening/reading on a separate thread. I will attach the code after I clean it.

Iniciar sesión para comentar.

Prannay Jain
Prannay Jain el 7 de Abr. de 2017
You have mentioned that setting the 'Interruptible' flag to 'on' does achieve your expected behavior. Then what is the part that you are not getting? Can you explain more on this?
I also want to confirm if you have Instrument Contol Toolbox license with your MATLAB.
  1 comentario
Yu-Chih Tung
Yu-Chih Tung el 8 de Abr. de 2017
Hi Prannay,
I do have the license (from University of Michigan). The Interruptible flag does help to keep UI usable while having some processing on the callback, but this flag DON'T work if there is a tcpip socket operation in the callback.

Iniciar sesión para comentar.


Community Treasure Hunt

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

Start Hunting!

Translated by