gui run command only if another input has been done
8 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
采纳的回答
Image Analyst
2015-3-10
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 个评论
Image Analyst
2015-3-10
To disable a button:
set(handles.pushbutton3, 'Enable', 'off'); % 'on' to enable it again.
更多回答(1 个)
Adam
2015-3-10
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 个评论
Adam
2015-3-10
编辑:Adam
2015-3-10
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!