필터 지우기
필터 지우기

creating a warning dialog that wait until the user press ok, if the user press x the execution stops

조회 수: 41 (최근 30일)
Im writing a code that creates xlsx file (using writetable function)
I want that if thier is a xlsx file with the same name a warning alert will pop and tell the user: their are files with the same name, are you sure you want to override them? If the user press ok -> the execution of the code continues. If the user press X (get out from the warning dialog window) the execition of the code stops.
in the title : "Wait for Button Press" they say that the following code should work:
f = figure('Position',[500 500 400 300]);
c = uicontrol('String','Continue','Callback','uiresume(f)');
uiwait(f)
disp('Program execution has resumed');
But it does not work. The uiwait, wait untii the user close the figure and not until the user press OK.
I want to do the following:
  1. user press OK -> execution continues
  2. user get out from the warning dialog window -> execution stops
It sounds very simple to me, but somehow I don't succeed to do it,
Someone did something similar?

채택된 답변

Simon Chan
Simon Chan 2023년 5월 14일
One possible way is to use function uniconfirm.
fig = uifigure;
selection = uiconfirm(fig,'Overwrite existing excel file','File Operation');
switch selection
case 'OK'
disp('Code to write data into excel file');
case 'Cancel'
return
end
close(fig);

추가 답변 (1개)

Jon
Jon 2023년 5월 11일
Does the behavior demonstrated in my example below do what you want?
for k = 1:10
if k ==3 % condition simulating duplicate file found, just for example
% Display confirmation for file overwrite
selection = questdlg('Overwrite duplicate file?','Duplicate File',...
'Overwrite','Exit','Exit');
if strcmp(selection,'Exit')
break
end
end
% display iteration (just to show something is happening
disp(k)
end
  댓글 수: 1
gili Maimon
gili Maimon 2023년 5월 14일
Hey Thank you very much!
the problem with this solution that the code after the for loop runs after the user press Exit, and only the code inside the loop do not run because of "break" function.
Do you know a function that will stop execution completly after the user will press Exit? thanl you!
I ruther not insert the full script (that locate anther the for loop) inside an if statment

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Interactive Control and Callbacks에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by