How to wait for a button to be pressed?
조회 수: 34 (최근 30일)
이전 댓글 표시
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!
댓글 수: 0
채택된 답변
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.
댓글 수: 0
추가 답변 (1개)
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
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
참고 항목
카테고리
Help Center 및 File Exchange에서 Simulink Environment Customization에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!