Set elements to hide\visible MATLAB Guide)

69 vues (au cours des 30 derniers jours)
Barak
Barak le 11 Nov 2022
Commenté : Barak le 15 Nov 2022
I have the following code in my GUI (guide) and I want to turn visible\hidden inputFld3 base on the radio button user selected .
I tried the following if block and set function but either I am doingit wrong or I am not placing it uder the correct sub-function of the GUI code.
  • What is the correct code?
  • Under which function the code to hide or unhide should be place?
Assistnace is much appriciated
if handles.baseHeightRbtn == 1
set(handles.inputFld3, 'Visible', 'off')
end
% My GUI code:
function varargout = testHideAndShowByRadioBtnV2(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @testHideAndShowByRadioBtnV2_OpeningFcn, ...
'gui_OutputFcn', @testHideAndShowByRadioBtnV2_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before testHideAndShowByRadioBtnV2 is made visible.
function testHideAndShowByRadioBtnV2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% Choose default command line output for testHideAndShowByRadioBtnV2
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes testHideAndShowByRadioBtnV2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = testHideAndShowByRadioBtnV2_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
function inputFld1_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function inputFld1_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function inputFld2_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function inputFld2_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function inputFld3_Callback(hObject, eventdata, handles)
% --- Executes during object creation, after setting all properties.
function inputFld3_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in baseHeightRbtn.
function baseHeightRbtn_Callback(hObject, eventdata, handles)
function figure1_CreateFcn(hObject, eventdata, handles)
function selectPanel_CreateFcn(hObject, eventdata, handles)
  2 commentaires
Rik
Rik le 11 Nov 2022
For general advice and examples for how to create a GUI (and avoid using GUIDE), have look at this thread.
There doesn't seem to be anything wrong with the code, although it doesn't provide the functionality to turn the visibility back on. Can you confirm you put those first 3 lines somewhere in your m-file, and not at the top of the m-file generated by GUIDE?
Barak
Barak le 11 Nov 2022
Hi Rik,
I indeed placed the if block under thus function:
function baseHeightRbtn_Callback(hObject, eventdata, handles)
but nothing seems to happen to the componnent I am trying to hise when user select that radio button 'baseHeightRbtn'
Maybe it is the wrong sub-function to place it. I also tried to move it under other functions but none seems to pick on that if-block code.

Connectez-vous pour commenter.

Réponse acceptée

Jan
Jan le 11 Nov 2022
The action should be done inside the callback function, which is called, when the element is clicked:
function baseHeightRbtn_Callback(hObject, eventdata, handles)
if handles.baseHeightRbtn == 1
set(handles.inputFld3, 'Visible', 'off');
end
end
  6 commentaires
Jan
Jan le 12 Nov 2022
Modifié(e) : Jan le 12 Nov 2022
@Walter Roberson: Really?! So GUIDE stuck at the programming style of Matlab R13 (6.5) from 2003? Thanks for this clarification. So I hope that the modern AppDesigner will be developped more dynamically.
Barak
Barak le 15 Nov 2022
@Jan sorry for the late answer and late acceptance of your answer as the one that solved it.
I did not add the 2nd 'end' as @Rik mentioned in his comment it causes the Guide to terminate with errors.
The initial value was indeed 1, so toggling between the radio buttons did not hide it. I switch the orderand now it works. Thanks.

Connectez-vous pour commenter.

Plus de réponses (1)

Image Analyst
Image Analyst le 12 Nov 2022
The function I use to disable buttons are attached.
For example
% Disable all controls.
WasEnabled = DisableControls(handles, 'Watch');
% Re-enable all controls.
EnableControls(WasEnabled);
You can change the Enabled to Visible if you want to hide or show them instead of gray them out.

Catégories

En savoir plus sur Migrate GUIDE Apps dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by