Hey, I am MArc and I have a question about a GUI. have this GUI interface:
I want to achieve that when clicking both pink clickboxes I want to make all the red handles disappear. I tried with a function using the button 2, like this:
methods (Access = private)
function Button2Pushed(app, event,handles)
value1= get(handles.PRETESSATRECTECheckBox,'Value')==0;
value2= get(handles.PRETESSATENCASTAMENTCheckBox,'Value')==0;
value3= get(handles.PRETESSATPARABLICCheckBox,'Value')==1;
value4= get(handles.ntramsCheckBox,'Value')==1;
if value1 && value2 && value3&& value4
set(handles.LmEditField_3, 'Visible', 'off');
set(handles.readelaseccim2EditField_2, 'Visible', 'off');
set(handles.LNmEditField_2, 'Visible', 'off');
set(handles.Inrciam4EditField_2, 'Visible', 'off');
set(handles.AmpladadelaseccimEditField_2, 'Visible', 'off');
set(handles.ncordonsEditField_2, 'Visible', 'off');
set(handles.Sigma_uMPaEditField_2, 'Visible', 'off');
set(handles.Sigma_yMPaEditField_2, 'Visible', 'off');
set(handles.CargapermanentKNm2EditField, 'Visible', 'off');
set(handles.SobrecargavariableKNm2EditField, 'Visible', 'off');
set(handles.SobrecargavariableKNEditField, 'Visible', 'off');
set(handles.PuntualKNEditField, 'Visible', 'off');
set(handlesD_vainammEditField, 'Visible', 'off');
else
set(handles.LmEditField_3, 'Visible', 'on');
end
end
end
But it does not work, it does nothing. I would like some advice, thank you

댓글 수: 6

Rik
Rik 2020년 8월 19일
Are you sure this code is reached?
Also, I would store all handles that need to be turned off in a single array. That way you only have one place to add the fields. Because you're using set you can simply input the array of objects and set the Visible property with a single line of code.
Marc Martinez Maestre
Marc Martinez Maestre 2020년 8월 19일
the code is located at the beginning, previous to the function of the push button, that is the one that performs all the operation required. It is a good idea to use one array, more simple, thank you. It is weird because the form of the code looks fine, but it does not work.
Rik
Rik 2020년 8월 19일
If you can't set a break point in this code, you can consider putting a msgbox at the start of the function. That way you can see if the function actually runs as expected.
cr
cr 2020년 8월 19일
Did you try relaunching Matlab?
First thing to try would be to check if the callback is actually being invoked when button2 is clicked. Second, see if any of value1,2,3,4 are 0 despite the checkbox statuses as shown in picture (are the underlying handles names of checkboxes correct?).
Yes, maybe is that it is not called, but I do not understand why, because it is located at the top of the code, previous to anything.
This is part of the code:
methods (Access = private)
function CheckBoxValueChanged(app, event)
% Get values of all checkboxes
cb(1) = app.PRETESSATPARABLICCheckBox.Value;
cb(2) = app.ntramsCheckBox.Value;
% Toggle the visibility flag depending on checkbox states
if all(cb)
visibleFlag = 'off';
else
visibleFlag = 'on';
end
app.LNmEditField_2.Visible = visibleFlag;
app.LmEditField_3.Visible = visibleFlag;
% etc...
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
value1= get(app.PRETESSATRECTECheckBox,'Value');
value2= get(app.PRETESSATENCASTAMENTCheckBox,'Value');
value3= get(app.PRETESSATPARABLICCheckBox,'Value');
value4= get(app.ntramsCheckBox,'Value');
if value1==0 && value2==0 && value3==0
msgbox(sprintf('Enter a valid selection'),'Error','error');
end
if value1==1
%---------------------------------------------------------------------------------------------------------------------------------------------------%
%-------------------------------------------------------------------PRETESSAT RECTE-----------------------------------------------------------------%
%---------------------------------------------------------------------------------------------------------------------------------------------------%
% Lllegir els valors previs
h_var = app.hcmEditField.Value;
end
end
So, it should be called, shouldn't it?
Rik
Rik 2020년 8월 20일
Change this
function CheckBoxValueChanged(app, event)
to this
function CheckBoxValueChanged(app, event)
uiwait(msgbox('the function was called'));
Then you know for sure if it is called or not.

댓글을 달려면 로그인하십시오.

 채택된 답변

Adam Danz
Adam Danz 2020년 8월 19일

0 개 추천

"I want to achieve that when clicking both pink clickboxes I want to make all the red handles disappear. "
The function you shared requires two checkboxes to be not-selected and two to be selected. That doesn't match your description.
Also, the checkbox detection needs to happen when any of the applicable checkboxes are toggled. If the detection only exists in 1 checkbox callback, toggling the other one will have no effect.
To avoid having 2 copies of the same code, assign the same CheckBoxValueChanged function to each applicable checkbox.
function CheckBoxValueChanged(app, event)
% Get values of all checkboxes
cb(1) = app.CheckBox.Value;
cb(2) = app.CheckBox2.Value;
% Toggle the visibility flag depending on checkbox states
if all(cb)
visibleFlag = 'off';
else
visibleFlag = 'on';
end
app.LNmEditField_2.Visible = visibleFlag;
app.LmEditField_3.Visible = visibleFlag;
% etc...
end

댓글 수: 5

Thank you so much Adam, that is exactly what I meant. I tried it but it does not do anything, could you check this part of the code, because maybe i wrote it in the wrong place.
methods (Access = private)
function CheckBoxValueChanged(app, event)
% Get values of all checkboxes
cb(1) = app.PRETESSATPARABLICCheckBox.Value;
cb(2) = app.ntramsCheckBox.Value;
% Toggle the visibility flag depending on checkbox states
if all(cb)
visibleFlag = 'off';
else
visibleFlag = 'on';
end
app.LNmEditField_2.Visible = visibleFlag;
app.LmEditField_3.Visible = visibleFlag;
% etc...
if cb(1)==1
msgbox(sprintf('Vector longitud de tram i repartida han de tenir la mateixa longitud'),'Error','error');
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
value1= get(app.PRETESSATRECTECheckBox,'Value');
value2= get(app.PRETESSATENCASTAMENTCheckBox,'Value');
value3= get(app.PRETESSATPARABLICCheckBox,'Value');
value4= get(app.ntramsCheckBox,'Value');
if value1==0 && value2==0 && value3==0
msgbox(sprintf('Enter a valid selection'),'Error','error');
end
if value1==1
%---------------------------------------------------------------------------------------------------------------------------------------------------%
%-------------------------------------------------------------------PRETESSAT RECTE-----------------------------------------------------------------%
%---------------------------------------------------------------------------------------------------------------------------------------------------%
% Lllegir els valors previs
h_var = app.hcmEditField.Value;
end
end
Thank you in advance
Adam Danz
Adam Danz 2020년 8월 20일
How did you assign the callback functions?
If you attach the mlapp file I can take a look at what's wrong. I have a feeling the callback functions were not assigned properly to the checkboxes.
Marc Martinez Maestre
Marc Martinez Maestre 2020년 8월 20일
Thank you
Adam Danz
Adam Danz 2020년 8월 20일
Here are the steps you need to take to fix the proble.
1) Add ValueChangeFcn callback function to one of the check boxes. Right-click the checkbox to show the menu you see below. That will add a new function in Code View.
2) Set up the function I provided in my answer. Note that the function in my answer is a demo. You'll need to understand what's going on in that function and adapt it to your needs. If you have specific questions with that, I can help.
3) Go back to Design Mode and right-click the other checkbox. This time, instead of adding a new ValueChangeFcn, you're going to select an existing callback, and select the function you just added.
4) Clean up the old stuff in your code.
To test it, I used the first 2 checkboxes to turn off/on a few components in the right column.
Marc Martinez Maestre
Marc Martinez Maestre 2020년 8월 24일
Thank you soo much, really awesome answer and really guided. Thank you

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ruger28
Ruger28 2020년 8월 19일

1 개 추천

As suggested above, make all of the items you would like to "disappear" (turn visibility off) by lumping them together in a single handle. Below is a quick example I threw together.
% in the opening function before gui is made visible
handles.MyEditBoxes = [handles.edit1,handles.edit2,handles.edit3,handles.edit4,handles.edit5];
Then for your pushbutton, get the values of the two checkboxes, and AND them.
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
value1 = get(handles.checkbox1,'Value');
value2 = get(handles.checkbox2,'Value');
if value1 && value2
set(handles.MyEditBoxes,'Visible','off');
else
set(handles.MyEditBoxes,'Visible','on');
end
This will hide/unhide the items in MyEditBoxes.

댓글 수: 1

Marc Martinez Maestre
Marc Martinez Maestre 2020년 8월 20일
Thank you Ruger28 but, how would it be to set the visibility off only by clicking two checkboxes?
Thank you

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Data Type Identification에 대해 자세히 알아보기

제품

릴리스

R2020a

태그

Community Treasure Hunt

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

Start Hunting!

Translated by