The following code will give you a while loop which can be stopped by clicking on the pop-up figure. It makes use of a global variable and a quick and dirty dialogue with a callback. Consider consulting the documentation for dialogs to make an optimised design.
global gStop % Global variable to stop while loop
gStop = false;
% Create a dialogue which when clicked calls the 'stopStream' function
stopBtn = dialog('Name', 'Click to Stop', "ButtonDownFcn",@stopStream);
drawnow
% Create while loop which runs until global gStop is true
while ~gStop
% Receive from SDR
drawnow % Gives dialog a chance to call stopStream and set the global variable
end
close(stopBtn) % Close once stopped
function stopStream(~,~,~)
% Sets global gStop to true
global gStop
gStop = true;
end
save capturedDataFile.mat myData
For large quantities of data, and perhaps to write to a single file each while loop iteration consider using the baseband file writer.