gui run command only if another input has been done

10 views (last 30 days)
Good Morning I have created a GUI with various push_buttons and list-boxes. I want to run, for example, a command from a list-box only if pushbutton2 has executed (Otherwise, in fact, it gives me an error and I cannot keep selecting other buttons)
It should be something lke:
if-----> got value of pushbutton2
run listbox1
else
(don't run)
I know it's a bit rough, but I'm still a Noob!

Accepted Answer

Image Analyst
Image Analyst on 10 Mar 2015
You could use a global variable.
In your OpeningFcn(), have this:
global pushButton2Pushed;
pushButton2Pushed = false;
In your pushButton2_Callback(), have this:
global pushButton2Pushed;
pushButton2Pushed = true;
In any other function where you want to check if push button 2 has been pushed, have this:
global pushButton2Pushed;
if pushButton2Pushed
% Do something
end
  1 Comment
Image Analyst
Image Analyst on 10 Mar 2015
To disable a button:
set(handles.pushbutton3, 'Enable', 'off'); % 'on' to enable it again.

Sign in to comment.

More Answers (1)

Adam
Adam on 10 Mar 2015
Do you mean you want to run the command when you press pushbutton2? In which case just put it in the callback for pushbutton2.
Or do you mean that at some point later on, triggered by some other button, you want to run whatever is in listbox1 only if pushbutton2 has been previously pressed? In this case you can just store a boolean on the handles structure in pushbutton2's callback, although I assume this dependency means that something else is stored when pushbutton2 is pressed so if that something is on handles just initialise it to [] in the OpeningFcn and then test it for emptiness in the condition you use above.
Often I disable buttons on my GUIs until it is valid to press them, but I am fussy - it isn't necessary, I just don't like giving someone a GUI in which they can press buttons in an order that makes the code crash, unless it is a quick and dirty demo.
  2 Comments
ludvikjahn
ludvikjahn on 10 Mar 2015
Edited: ludvikjahn on 10 Mar 2015
how coul you o that? I mean, disable buttons until is vald to press them (or also select items for a listbox, as the same)?
That's exactly what i was looking for!!
Adam
Adam on 10 Mar 2015
Edited: Adam on 10 Mar 2015
You have to program the logic for disabling yourself, using the 'Enable' property of uicontrol objects.
In a complex GUI I usually do this with a class that supports various states of the GUI that I define and disables and enables components depending on that state. That can be quite a lot of work, but we are releasing executables to external companies so a 'happy go lucky' approach doesn't really suit!
For a less complicated GUI I just manually disable/enable relevant buttons in the callbacks of others. If it is simply a one-time enabling that is required I disable it in GUIDE and then enable once it in the relevant callback.
As far as the idea of only doing something if another button has been pressed though I would advise against thinking about it that way as I mentioned above.
Whether or not a button has been pressed is a GUI detail. What is important is what happened when you pressed that button that makes it a dependency for the later functionality. You should be creating logic based on testing for the presence or validity of that rather than introducing booleans based on whether a GUI button was pressed or not.
e.g. in the OpeningFcn:
handles.inputData = [];
...
guidata( hObject, handles );
In your pushbutton callback, for example
handles.inputData = load( 'data.mat' );
guidata( hObject, handles )
Obviously that is just an example. I don't know what you are specifically doing under your pushbutton.
Then wherever you need to test the logic:
if ~isempty( handles.inputData )
run listbox1
else
(don't run)
end

Sign in to comment.

Categories

Find more on Interactive Control and Callbacks 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!