필터 지우기
필터 지우기

How to wait for a button to be pressed?

조회 수: 32 (최근 30일)
Oliver Lestrange
Oliver Lestrange 2020년 8월 22일
답변: Image Analyst 2020년 8월 22일
Hi,
I have the following code:
% --- Executes on button press in insertAntenna.
function insertAntenna_Callback(hObject, eventdata, handles)
% hObject handle to insertAntenna (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Coloca a antena nas coordenadas seleccionadas pelo ginput %
handles = guidata(hObject)
[x_gNB, y_gNB] = ginput(1);
handles.nAntena = handles.nAntena + 1
Antena_x = x_gNB;
Antena_y = y_gNB;
rectangle('Position', [(Antena_x - handles.Rwidth/2) (Antena_y - handles.Rheight/2)...
handles.Rwidth handles.Rheight], 'FaceColor', 'r','LineStyle', 'none');
uiwait(waitfor(sectorAngle_Button_Callback(hObject, eventdata, handles))); % Wait for the respective sector
handles.Antena(handles.nAntena).Antena_x = Antena_x;
handles.Antena(handles.nAntena).Antena_y = Antena_y;
handles.Antena(handles.nAntena).Info = handles.Antena(1).Info;
handles.Antena(handles.nAntena).Altura = handles.Antena_Altura;
handles.Antena(handles.nAntena).Direction = handles.AntenaDirection;
guidata(hObject, handles)
% --------------------------------------------------------------------------------------------------------------------------
% --- Executes on selection change in sectorAngle.
function sectorAngle_Callback(hObject, eventdata, handles)
% hObject handle to sectorAngle (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns sectorAngle contents as cell array
% contents{get(hObject,'Value')} returns selected item from sectorAngle
contents = cellstr(get(hObject,'String')) % Options of popupmenu
selected = contents{get(hObject,'Value')} % Value selected by the user through popupmenu
if strcmp(selected,'Sector1')
handles.AntenaDirection = 300;
elseif strcmp(selected,'Sector2')
handles.AntenaDirection = 180;
else
handles.AntenaDirection = 60;
end
guidata(hObject, handles)
% --------------------------------------------------------------------------------------------------------------------------
% --- Executes during object creation, after setting all properties.
function sectorAngle_CreateFcn(hObject, eventdata, handles)
% hObject handle to sectorAngle (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
s1 = 'Sector1';
s2 = 'Sector2';
s3 = 'Sector3';
sectors = char(s1, s2, s3);
set(hObject,'string',sectors)
% --------------------------------------------------------------------------------------------------------------------------
% --- Executes on button press in sectorAngle_Button.
function sectorAngle_Button_Callback(hObject, eventdata, handles)
% hObject handle to sectorAngle_Button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
In the line
uiwait(waitfor(sectorAngle_Button_Callback(hObject, eventdata, handles))); % Wait for the respective sector
I want to wait for the button sectorAngle be pressed in order to handles.AntenaDirection value.
What I am doing wrong?
Thanks!

채택된 답변

Image Analyst
Image Analyst 2020년 8월 22일
Don't put that code
s1 = 'Sector1';
s2 = 'Sector2';
s3 = 'Sector3';
sectors = char(s1, s2, s3);
set(hObject,'string',sectors)
into the CreateFcn. I never put anything it there. Just leave the createfcn function alone. Put it into the sectorAngle_Button_Callback callback or your OpeningFcn function.
And don't have the uiwait(waitfor()) line of code - it's not necessary since the button is already waiting for you to click it. If you still have trouble, attach both the .fig file and .m file.

추가 답변 (1개)

Adam Danz
Adam Danz 2020년 8월 22일
Place this in the line where you want execution to pause
uiwait(handles.figure1) % use your figure handle
Then, from within the button's callback function, add the line,
uiresume(handles.figure1) % use your figure handle
  댓글 수: 5
Adam Danz
Adam Danz 2020년 8월 22일
편집: Adam Danz 2020년 8월 22일
That's clearer.
What is the value of selected just before the conditional statements?
It may be the case that none of your conditions are met and if the initial value is 60, that value wouldn't change.
By the way, a switch/case block is sometimes a better way to organize discrete options than if/else statements.
if swtich(lower(selected))
case 'sector1' % lowercase
handles.AntenaDirection = 300;
case 'sector2' % lowercase
handles.AntenaDirection = 180;
otherwise
handles.AntenaDirection = 60;
end
Oliver Lestrange
Oliver Lestrange 2020년 8월 22일
편집: Oliver Lestrange 2020년 8월 22일
"What is the value of selected just before the conditional statements" - Is Sector1, Sector2 or Sector3, depending of which I selected in the pop-up menu, just like is supposed to.
Thanks for the switch/case suggestion.

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

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by