Stop an infinite loop with a key
조회 수: 2 (최근 30일)
이전 댓글 표시
Hey guys, I have this infinite loop and I want to exit the while loop and continue the code when I press the cancel button in the waitbar, but this button isnt working, what can I do?
% Start the module
dev.start();
fprintf('Start of LimeSDR\n');
tic;
start_time=datetime('now','Format','dd-MMM-uuuu HH:mm:ss.SSS');
hWaitbar = waitbar(0,'Receiving Samples', 'Name', 'Progress', ...
'CreateCancelBtn','setappdata(gcbf,''canceling'',1)');
setappdata(hWaitbar,'canceling',0);
while true
% Receive samples on RX1 channel
indRx1 = 1; % index of the last received sample
[samples1, ~, samplesLength1] = dev.receive(Fs*Ts,1);
bufferRx1(indRx1:indRx1+samplesLength1-1) = samples1;
TT=array2timetable(samples1,'SampleRate',Fs,'StartTime',start_time);
writetimetable(TT,'Timetable_Rx1.txt','Delimiter','bar');
type Timetable_Rx1.txt
pause(1);
if getappdata(hWaitbar,'canceling')
% Stop the if cancel button was pressed
disp('Stopped by user');
break;
end
delete(hWaitbar);
end
% Cleanup and shutdown by stopping the RX stream and having MATLAB delete the handle object.
dev.stop();
tempo_rececao=toc;
stop_time=datetime('now','Format','dd-MMM-uuuu HH:mm:ss.SSS');
clear dev;
fprintf('Stop of LimeSDR\n');
댓글 수: 0
채택된 답변
Jan
2022년 6월 22일
편집: Jan
2022년 6월 22일
You delete the waitbar in the first iteration:
delete(hWaitbar);
Remove this line. or move it behind the loop.
I cannot imagine what you call "does not work", because if you delete the complete waitbar, there is no button to press at all. Maybe you have some orphaned waitbars from former trials?
댓글 수: 2
Jan
2022년 6월 23일
Explaining, what does not happen, is less useful thanto explain what is happening.
if getappdata(hWaitbar,'canceling')
Does this stop with an error, because there is no field "canceling" before you press the button?
Defining callbacks as CHAR vectors is not recommended. Such strings are evaluated in the base workspace. If you have redefined e.g. "gcbf" as a variable, the callback will fail. Prefer function handles:
hWaitbar = waitbar(0,'Receiving Samples', 'Name', 'Progress', ...
'CreateCancelBtn', @(h, e) setappdata(ancestor(h, 'figure'), canceling', 1));
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Dialog Boxes에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!