Change button Enable after two other buttons have been pressed

Hello - I'm trying to get two buttons to become Enabled after two other buttons have been pressed. I'm not sure what I'm doing wrong here, but I'm pretty new to MATLAB and am likely making a silly mistake. This was a procedurally made GUI.
I set up the handles structure:
fighandl=figure;
handles = guihandles(fighandl);
%button visibility tracker
handles.firstVis = 0;
handles.secondVis = 0;
guidata(fighandl,handles);
Here's the code for the button creation (only including what's necessary):
push1=uicontrol('Style','pushbutton',...
'String','Play Excerpt 1',...
'Units','Normalized',...
'Position',[0.05 0.6 0.4 0.3],...
'Callback',@push1_Callback);
push2=uicontrol('Style','pushbutton',...
'String','Play Excerpt 2',...
'Units','Normalized',...
'Position',[0.55 0.6 0.4 0.3],...
'Callback',@push2_Callback);
handles.push3=uicontrol('Style','pushbutton',...
'String','Prefer Excerpt 1',...
'Position',[0.05 0.3 0.4 0.2],...
'Enable','off',...
'Callback',@push3_Callback);
handles.push4=uicontrol('Style','pushbutton',...
'String','Prefer Excerpt 2',...
'Position',[0.55 0.3 0.4 0.2],...
'Enable','off',...
'Callback',@push4_Callback);
And here are the callback functions (I'm only including the two that play a role in enabling push3 and push4, which are virtually identical):
function push1_Callback(push1, eventdata, handles, whichsound)
handles = guidata(gcf);
%tracker to see if both buttons have been pressed
handles.firstVis = handles.firstVis+1;
if (handles.firstVis>=1 && handles.secondVis>=1)
%this is not working
%it will display 'yay' but not update button visibility.
disp('Yay');
set(handles.push3,'Enable','on');
set(handles.push4,'Enable','on');
end
%store info
guidata(gcf,handles)
And the second function
function push2_Callback(push2, eventdata, handles, whichsound)
% Get the structure using guidata in the local function
handles = guidata(gcf);
%tracker to see if both buttons have been pressed
handles.secondVis = handles.secondVis+1;
if (handles.firstVis>=1 && handles.secondVis>=1)
%this is not working
%it will display 'yay' but not update button visibility.
disp('Yay');
set(handles.push3,'Enable','on');
set(handles.push4,'Enable','on');
end
guidata(gcf,handles)

7 Comments

I should've included my error, sorry.
Yay
Reference to non-existent field 'push3'.
Error in push2_Callback (line 30)
set(handles.push3,'Enable','on');
Error while evaluating UIControl Callback
The callbacks look like GUIDE-generated code. If so, then why are you making your own controls with the uicontrol() function call???
Hi there - Sorry, they aren't GUIDE generated. I pulled some of what worked from previous iterations of this project and stupidly included that commented section. I considered not including it here (and shouldn't have done so).
Each function (push1_Callback and push2_Callback) is its own .m file that the GUI looks to for the function call.
Edit 8/24: I removed the sections that appeared to be GUIDE generated.
Are you doing
guidata(fighandl,handles);
after you assign the handles.push* values?
handles = guihandles(fighandl);
%audio files
handles.y1 = y1;
handles.y2 = y2;
handles.fs1 = fs1;
handles.fs2 = fs2;
%button visibility tracker
handles.firstVis = 0;
handles.secondVis = 0;
%button press tracker
handles.firstHits = 0;
handles.secondHits = 0;
%button timer tracker
handles.timerStart = 0;
handles.elapsedTime = 0;
%save into gui structure
guidata(fighandl,handles);
% create some text instructions in the figure
text1=uicontrol('Style','text',...
'String','Listen to both sounds and choose preferred sound',...
'FontSize',18,...
'FontWeight','bold',...
'Units','Normalized',...
'Position',[0.05 0.05 0.85 0.2]);
After this I start defining pushbuttons (outlined above). A lot of these other handles setup bits aren't the problem but are used for other things (e.g. playing sound).
The guidata(fighandl,handles); has to go after your last assignment into the handles structure
Jeez, that makes sense. Thank you, that worked beautifully! I just recently wrapped my head around the handles structure, so it's a learning process.
Is there a downside to having this at the end of the GUI.m file? That's where I moved it and everything was happy.
Edit: Happy to give you an answered question or anything that might help you - thanks for the help!

Sign in to comment.

Answers (0)

Products

Release

R2015a

Asked:

on 17 Aug 2018

Edited:

on 24 Aug 2018

Community Treasure Hunt

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

Start Hunting!