Hello,
I've 4 popup menu and 1 push button,
I want to, once i choise the 1st value in popup1, the 1st one in popup2, the 1st in popup3 and the 1st in popup4, the push button will be Enable.
Thanks,

4 Kommentare

Adam Danz
Adam Danz am 22 Mai 2020
Pneu 94's answer moved here as a comment.
I use a guide, and I'm programming the gui manually
Adam Danz
Adam Danz am 22 Mai 2020
If you'd like to use the approach in my answer, it would be easy to adapt it to GUIDE. Let me know if you have a specific question about how to do that.
Pneu  94
Pneu 94 am 22 Mai 2020
I really begin in Gui, so I don't know a lot of things, My problem is to enable a push button after the 4 conditions of the pop buttons are set Thanks
Adam Danz
Adam Danz am 22 Mai 2020
See my updated answer. Importantly, you'll have to change the handles to match your handle names.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Image Analyst
Image Analyst am 22 Mai 2020
Bearbeitet: Image Analyst am 22 Mai 2020

0 Stimmen

Try this (assuming GUIDE - replace handles with app if using AppDesigner)
if handles.popup1.Value == 1 && handles.popup2.Value == 1 && handles.popup3.Value == 1 && handles.popup4.Value == 1
handles.button1.Enable = 'on';
else
handles.button1.Enable = 'off';
end

12 Kommentare

Adam Danz
Adam Danz am 22 Mai 2020
Well, that worked out :D
I wrote my answer, assuming AppDesigner, before seeing your answer assuming Guide.
Image Analyst
Image Analyst am 22 Mai 2020
Bearbeitet: Image Analyst am 22 Mai 2020
I'd like to use App Designer but not having the command window, the workspace, and the code all in the same window (but different panels like we have it in MATLAB) is a deal breaker for me.
Pneu  94
Pneu 94 am 22 Mai 2020
thanks for answering, didnt word for me,
function pushbutton1(hObject, ~)
if handles.xpop.Value == 1 & handles.popup2.Value == 1 & handles.popup3.Value == 1 & handles.popup4.Value == 1
handles.button1.Enable = 'on';
else
handles.button1.Enable = 'off';
end
What button do you want to enable or disable? Surely it's not pushbutton1. I mean, if you disabled it when you went in there, then how would it ever get disabled? Doesn't make sense, unless you enabled it somewhere else.
So you need to put that code in the callback for each of the popups. You can put the code into a function if you want.
function handles = PopupClicked(handles)
if handles.popup1.Value == 1 && handles.popup2.Value == 1 && handles.popup3.Value == 1 && handles.popup4.Value == 1
handles.button1.Enable = 'on';
else
handles.button1.Enable = 'off';
end
Then call that function from each popup callback, like this:
%=================================================================================
% --- Executes on selection change in popupmenu1.
function popupmenu1_Callback(hObject, eventdata, handles)
handles = PopupClicked(handles);
guidata(hObject, handles);
%=================================================================================
% --- Executes on selection change in popupmenu2.
function popupmenu2_Callback(hObject, eventdata, handles)
handles = PopupClicked(handles);
guidata(hObject, handles);
%=================================================================================
% --- Executes on selection change in popupmenu3.
function popupmenu3_Callback(hObject, eventdata, handles)
handles = PopupClicked(handles);
guidata(hObject, handles);
%=================================================================================
% --- Executes on selection change in popupmenu4.
function popupmenu4_Callback(hObject, eventdata, handles)
handles = PopupClicked(handles);
guidata(hObject, handles);
Adam Danz
Adam Danz am 22 Mai 2020
Bearbeitet: Adam Danz am 22 Mai 2020
Why return the handles structure and use guidata to update the handles? The handles structure should retain the changes made to GUI objects within the local PopupClicked function.
Image Analyst
Image Analyst am 22 Mai 2020
No, I don't believe it does and I've been burned by that before. Remember when you pass it it's pass by value. So it's a local copy of handles. Anything you do to it will be lost after exiting unless you call guidata, or you pass out the new handles and some other function somewhere in the call stack calls it with the updated handles structure. For example if you did something to handles, like add a field or set some property, then that setting doesn't necessarily "stick" once you're out of the function. It will have the old value. If you set a listbox index, it does seem to take effect on the GUi immediately however I've seen in the past when you try to get the property (index) it will have the old one, not the current one.
Maybe guidata() is no longer needed. Well anyway since they've quit development of GUIDE and want everyone to use App Designer, I think it's not needed with App Designer since app designer has the "app" structure variable, which is a global variable, which I think means changes to it take effect everywhere immediately.
Pneu  94
Pneu 94 am 22 Mai 2020
Thanks guys, i think i will not do this like that, i will set the push button enable,
but , i have to make some think which allow me to open another gui, when i click the push button.
and the new gui depends to what i chosed in the pop up menu.
Do you have any idea Please.
Thanks
Adam Danz
Adam Danz am 22 Mai 2020
Bearbeitet: Adam Danz am 22 Mai 2020
@Image Analyst, when an object handle is passed into another function, the function copies only the handle, not the object identified by that handle. Both handles (original and local copy) refer to the same object. When the function modifies the data referred to by the object handle, those changes are accessible from the handle variable in the calling workspace without the need to return the modified object (explained in the documentation).
Attached is a GUIDE GUI demonstrating this using 3 popup menus that change the Enable property of a PushButton when the first item in all lists is selected.
If a field is added or removed from the handles structure or if a field is changed that isn't an object in the GUI, then it would be necessary to return the handles structure and reassign it to the GUI.
@Pneu 94 , it's important to understand and clearly explain the problem before people invest time into a solution that doesn't address the unexplained problem. I don't undrestand your updated description. The goal should be crystal clear to you before requesting help.
Pneu  94
Pneu 94 am 22 Mai 2020
The objective is to open a new gui that i have defined.
this new gui will be opened just when i clic on the start button (push button),
as i said, i have 4 pop up menu and that Start button,
That what i did until now:
function comp_type(hObject, eventdata, handles) % Callback of the pop up menu
contents = cellstr(get(hObject,'String'));
popchoice = contents(get(hObject,'Value'));
if (strcmp(comp,'Capacitor')) % first choice in pop up menu1
disp('Start');
disp('*****************')
go_SEC_C_Callback();
%%%%
that allow me to open the new gui but not when i clic Start ( it did that just when i choise the first choice in pop1).
So i want to do the following steps:
1- choise the first choice in pop1;
2- choise the 1st choice in pop2.
3- choice the fisrt choice in pop3
4- choice the fisrt choice in pop4
5-clic Start to open the new gui
thanks
Image Analyst
Image Analyst am 23 Mai 2020
Adam, thanks for the explanation. Maybe that's the situation I was remembering (adding a field) where I got burned and so now I just do it all the time, even if it's not needed. So what is the point of guidata() then? Why do we ever need to call it?
Pneu, if you need to share variables and controls with a sub-gui that you popup/create, then see the bottom part of this FAQ entry: https://matlab.fandom.com/wiki/FAQ#How_can_I_share_data_between_callback_functions_in_my_GUI.28s.29.3F
I use guidata() to set and retrieve values from non-graphic-handle variables stored somehwere in the app or if I want to add a field to the handles structure in GUIDE GUIs.
Example,
f = figure();
data.a = 9999;
guidata(f,data)
localFunc(f);
disp(guidata(f))
function localFunc(f)
% Add 1 to data.a
data = guidata(f);
data.a = data.a + 1;
guidata(f,data)
end
Image Analyst
Image Analyst am 24 Mai 2020
OK, makes sense. Thanks.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Adam Danz
Adam Danz am 22 Mai 2020
Bearbeitet: Adam Danz am 22 Mai 2020

0 Stimmen

Each of the popup menus should have a callback function that checks the selection of all 4 popup menus. Use a conditional statement that enables the push button when the selection of each popup menu is 1.
For AppDesigner:
I assume the 'popup menu' is what Matlab calls a 'Drop down' menu.
Step 1: create a new function in the app that checks all of the dropdown menus.
From Code View > Code Browser > Functions, select the gree "+" to add this function. You must replace app.DropDown with your handles.
function checkAllDropDownMenus(app)
% Get values of all DropDown menus
getValueIdx = @(h)find(strcmp(h.Items, h.Value));
idx1 = getValueIdx(app.DropDown);
idx2 = getValueIdx(app.DropDown2);
idx3 = getValueIdx(app.DropDown3);
idx4 = getValueIdx(app.DropDown4);
% If all selections are 1, enable button
if isequal(1,idx1,idx2,idx3,idx4)
app.Button.Enable = 'on';
end
end
Step 2: assign a ValueChangedFcn to each drop down menu
The ValueChangedFcn for each dropdown menu should appear as below plus any other code you may already have for the callback function.
% Value changed function: DropDown
function DropDownValueChanged(app, event)
checkAllDropDownMenus(app);
end
For GUIDE GUIs the 2 steps would look like this
Step 1: add this function anywhere in your GUIDE GUI m-file. Replace the handles with your handle names.
function checkAllDropDownMenus(handles)
% Get values of all DropDown menus
idx1 = handles.popupmenu1.Value;
idx2 = handles.popupmenu2.Value;
idx3 = handles.popupmenu3.Value;
idx4 = handles.popupmenu4.Value;
% If all selections are 1, enable button
if isequal(1,idx1,idx2,idx3,idx4)
handles.pushbutton1.Enable = 'on';
end
Step 2: Each popup menu should have a callback function that you add from within GUIDE by right-clicking the popup menu > View Callbacks > Callback. That will add a function to your m-file if it doesn't already exist. Edit the added function so it appears as below.
function popupmenu1_Callback(hObject, eventdata, handles)
checkAllDropDownMenus(handles)

Kategorien

Mehr zu App Building finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by