MATLAB: Matlab socket is not interruptible

interruptibleMATLABsocketui

Hi,
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 http://www.mathworks.com/help/matlab/creating_guis/callback-sequencing-and-interruption.html 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...',...
'Position',[250,320,270,50],...
'CloseRequestFcn',@close_waitbar);
port = 50005;
socket=tcpip('0.0.0.0', port, 'NetworkRole', 'server', 'Timeout', 60*60*24);
fprintf(2, '=== start wait connection to port = %d ===\n', port);
fopen(socket);
fprintf(2, '=== succeesfully get a connection to port = %d ===\n', port);
for i=1:10000,
if ishandle(h_wait)
action=fread(socket, 1, 'int8')
waitbar(i/10000,h_wait)
tic
fprintf('\nwait: %.1f: ', (i/10000)*100);
toc
else
break
end
end
% When waitbar reaches max, close it.
if ishandle(h_wait)
close(h_wait)
end
end
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?

Best Answer

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.