How to wait for a button to be pressed?

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

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.

More Answers (1)

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

Hi Adam,
Thanks a lot!
When it exits this part of code the handles.AntenaDirection stays again with the initialized value. Can you understand why this is happening, with the posted code?
% --- 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)
Thanks again
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.
I want to handles.AntenaDirection stores the value 300, 180 or 60, and then when I access it in
handles.Antena(handles.nAntena).Direction = handles.AntenaDirection;
on the insertAntenna_Callback function I have that value, and not the value with a initialize in the gui opening function.
Important notice that the sectorAngle_Callback function is before insertAntenna_Callback.
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
"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.

Sign in to comment.

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!