How to wait for a button to be pressed?
Show older comments
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!
Accepted Answer
More Answers (1)
Adam Danz
on 22 Aug 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 Comments
Oliver Lestrange
on 22 Aug 2020
Adam Danz
on 22 Aug 2020
What is handles.AntenaDirection? It looks like you're overwriting the handle variable with a numeric value instead of assigning the numeric value to the AntenaDirection object.
Instead,
handles.AntenaDirection.Value = 300;
but since I don't know what handles.AntenaDirection is, the Value property may not be the correct property.
Or, perhaps handles.AntenaDirection stores a value. In that case, I don't see where you're assigning that value to a GUI object.
Oliver Lestrange
on 22 Aug 2020
Edited: Oliver Lestrange
on 22 Aug 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
on 22 Aug 2020
Edited: Oliver Lestrange
on 22 Aug 2020
Categories
Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!