Set elements to hide\visible MATLAB Guide)
22 views (last 30 days)
Show older comments
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 Comments
Rik
on 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?
Accepted Answer
Jan
on 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 Comments
Jan
on 12 Nov 2022
Edited: Jan
on 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.
More Answers (1)
Image Analyst
on 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.
0 Comments
See Also
Categories
Find more on Migrate GUIDE Apps 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!