Set elements to hide\visible MATLAB Guide)

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 Kommentare

Rik
Rik am 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 am 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.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Jan
Jan am 11 Nov. 2022

0 Stimmen

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 Kommentare

Barak
Barak am 11 Nov. 2022
Bearbeitet: Barak am 11 Nov. 2022
I think you have an end command where it does not allow it in the guide code. the 2nd 'end' in your answer.
I placed it exactly like you did it in your answer and it did not work. When I toggle beetween the radio buttons and select the one that suppose to hide another element in the screen, nothing happens to it. it stays visible.
I am attachign the fig and function files
@Barak: No, the end is placed correctly. Using "end" to close function definitions is valid syntax for almost 25 years in Matlab. This "modern" style allows nested functions. But if one function in an M file has a closing end, all functions need this also.
"Nothing happens"? Are there messages in the command window? I'd expect some, because handles.baseHeightRbtn is not defined anywhere. Maybe this is the handle of the button? Then use its protery Value:
if handles.baseHeightRbtn.Value == 1
set(handles.inputFld3, 'Visible', 'off');
end
Examine such problems using the debugger: Set a breakpoint in the code (click in the bar on the left of the editor. Matlab stops there and you can check the values of the variables:
handles.baseHeightRbtn
Can this be 1?
Rik
Rik am 11 Nov. 2022
While it is true that closing functions with end is allowed for a very long time, GUIDE does not use that convention. One of the many reasons I dislike GUIDE. Unfortunately, since you cannot mix the two syntax styles, inserting a function that includes an end requires you to add an end to all other functions as well.
Walter Roberson
Walter Roberson am 11 Nov. 2022
And unfortunately once you have inserted the matching end statements, GUIDE cannot properly process the code, so you cannot modify the interface using GUIDE once you have put the end in.
Jan
Jan am 12 Nov. 2022
Bearbeitet: Jan am 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 am 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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 12 Nov. 2022

0 Stimmen

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.

Kategorien

Mehr zu Migrate GUIDE Apps finden Sie in Hilfe-Center und File Exchange

Produkte

Version

R2022b

Gefragt:

am 11 Nov. 2022

Kommentiert:

am 15 Nov. 2022

Community Treasure Hunt

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

Start Hunting!

Translated by