From: Mark de Jong on
Hi there,

I wrote an m-file which allows me to send an image over a USB-port to a transmitter module. Everything seemed to work and I made a nice GUI to make it easy to use, but now I have encountered a problem. Sending the data will take some time, so I want the user to be able to abort the transmission. In Matlab, I can simply use ctrl+c, but I want to create an executable from the GUI which must be able to abort the transmission too. So I made another GUI (sending.m), creating a small messagebox with one button: 'abort'. When you press this button, the messagebox closes.

In the main GUI, the program will send the data with a for-loop, fwriting 512 bytes every time. I wanted to check if the messagebox, called msg2 [= sending()] is still open with

ishandle(msg2)==0

every time the loop is executed and break from the for-loop when the messagebox is closed. This works fine with a simple for-loop. But when I use fwrite, this technique seems to fail.
Matlab opens msg2, but when the program enters the for-loop and starts transmitting data using fwrite, the 'abort' button fails. The window isn't even closed. My guess is the fwrite command uses too much CPU, so Matlab forgets all about the messagebox.

Does anyone know how to fix this problem? If there is another technique or if I have to use fwrite without a for-loop, than that's okay too. But please keep in mind that I have to send 1175040 bytes using an USB port.

Here are some relevant pieces of my code. Transmitter is a long vector containing all the data to be transmitted:

%%Transmit data with for loop and break code
s1 = serial('COM4', 'Baudrate',9600);
fopen(s1);
pause(1.1);

msg2 = sending();

L = length(Transmitter);
Windows = (L/512);

for k = 0:Windows
if strcmp(s1.TransferStatus,'idle') == 1
fwrite(s1, Transmitter(1+(k-1)*512:k*512));
if ishandle(msg2) == 0
break;
end
else
Windows = Windows + 1;
end
end


%% sending.m

function pushbutton1_Callback(hObject, eventdata, handles)
close(sending);


Please feel free to ask for more code or any other information! All help is very welcome!