How to wait for a button to be pressed?

34 visualizzazioni (ultimi 30 giorni)
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!

Risposta accettata

Image Analyst
Image Analyst il 22 Ago 2020
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.

Più risposte (1)

Adam Danz
Adam Danz il 22 Ago 2020
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 Commenti
Adam Danz
Adam Danz il 22 Ago 2020
Modificato: Adam Danz il 22 Ago 2020
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 il 22 Ago 2020
Modificato: Oliver Lestrange il 22 Ago 2020
"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.

Accedi per commentare.

Categorie

Scopri di più su Simulink Environment Customization in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by