Selecting radio button resets GUI [GUIDE, R2017b]

Hello,
I have a GUI with several edit boxes which sow the names of files which have been selected by the user. I aso have some radio buttons which dictate what operation to complete on the data which is loaded vvia the GUI.
When I have loaded the data and then click a radio button which was not the default, all the edit boxes clear and it seems as though the GUI is reinitialised. I have not used the radio button group call back for selectionChanged. All I do is check which radio button has been clicked when it comes to performing the calculations. Does anyone know why the radio button keeps reinitialising the GUI?
The code for initialising the GUI objects I have written is:
function defaultVals(hObject, handles)
%
% This function takes the handle to the GUI figure, and structure with
% handles and user data and sets the values of the various
% objects on the GUI.
% Call this function in the Openingfunction of the GUI to set initial
% values for the GUI objects.
set(handles.textPath,'string','No file loaded.'); % sets default value of textFile
set(handles.textFile,'string','No file loaded.'); % sets default value of textData
set(handles.checkbox1 , 'Enable','off'); % disable checkbox1;
I placed this function in the OpeningFcn. After using the dbstack (Thanks to Jan Simon's suggestion) I found the following call stack was produced when I placed 'dbstack' in my defaultVals function:
In VoltageResonance>defaultVals (line 272)
In VoltageResonance>VoltageResonance_OpeningFcn (line 58)
In gui_mainfcn (line 220)
In VoltageResonance (line 42)
In matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)VoltageResonance('trainConfigGroup_SelectionChangedFcn',get(hObject,'SelectedObject'),eventdata,guidata(get(hObject,'SelectedObject')))
It appears that the function matlab.graphics.internal.figfile.FigFile/read causes the GUI to be run again but I am not sure where that function arises from by changing a radio button. Kind regards, TJ

 Accepted Answer

Then the callback of the radio button contains some code, which initializes the contents of the GUI. Without seeing the code, we cannot guess, which command it is. But you can find this out easily using the debugger: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html Set a breakpoint in the callback and step through the code line by line. You will find, which command causes the reset. Finally remove this command.

9 Comments

Hi Jan,
So I went through and debugged the code and when I have no code in the radio button callbacks. However, I did notice where the problem arises. I have a initialise function which sets all the edit text boxes and enable status' for my GUI. I placed this function call into the following function:
% --- Executes just before VoltageResonance is made visible.
function VoltageResonance_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to VoltageResonance (see VARARGIN)
% Choose default command line output for VoltageResonance
handles.output = hObject;
defaultVals(hObject, handles);
% Update handles structure
guidata(hObject, handles);
When I select a different radio button for some reason the code then goes to the GUI OpeningFcn.
Not sure as to why the change in a radio button causes this function to execute.
I have played around a bit more and it seems when you click on a radio button, the code is interrupted and goes directly to the top of the m-file, in my case the GUI was called Voltage Resonance:
function varargout = VoltageResonance(varargin)
% VOLTAGERESONANCE MATLAB code for VoltageResonance.fig
% VOLTAGERESONANCE, by itself, creates a new VOLTAGERESONANCE or raises the existing
% singleton*.
%
% H = VOLTAGERESONANCE returns the handle to a new VOLTAGERESONANCE or the handle to
% the existing singleton*.
%
% VOLTAGERESONANCE('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in VOLTAGERESONANCE.M with the given input arguments.
%
% VOLTAGERESONANCE('Property','Value',...) creates a new VOLTAGERESONANCE or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before VoltageResonance_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to VoltageResonance_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help VoltageResonance
% Last Modified by GUIDE v2.5 04-Jan-2018 18:39:01
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @VoltageResonance_OpeningFcn, ...
'gui_OutputFcn', @VoltageResonance_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
So interestingly there is a handle to the OpeningFcn so anything there would be executed every time a radio button is selected. I am not able to find any information on why the radio button triggers the .m file to start over (or call the top GUI function).
And again, you can find this out easily using the debugger: Set a break point in this function and check, from where it is called. You find this either by the dbstack command in the command window or in the popupmenu in the editor.
Read the documentation about the debugger carefully. It is the best friend of programmers and you cannot work efficiently with Matlab, if you do not know how to debug code. Search in the net and in the documentation for "debugger" or "debug" to find out more details.
Hi Jan,
Thanks for the feedback so far. I have done a few tutorials using some of the debug tools and they are extremely useful. What I found was that OpeningFcn is called just before the GUI is visible. Then I carry on with my GUI actions and as soon as I click a radio button such that the selection changes the following function is called:
matlab.graphics.internal.figfile.FigFile/read>@(hObject,eventdata)VoltageResonance('trainConfigGroup_SelectionChangedFcn',get(hObject,'SelectedObject'),eventdata,guidata(get(hObject,'SelectedObject')))
This then calls my VoltageResonance function which is my main GUI, which subsequently calls the gui_OpeningFcn. I am still not sure why changing a radio button would result in that function 'matlab.graphics.internal.figfile.FigFile/read' being called. I will carry on hacking away to try figure this out. I am about a month into this project and would hate to have to restart it.
@TJ: You will find the reason for this behavior. Radio buttons should not trigger a reload. Maybe there is a "load" command hidden on the right side outside the visible area of the editor window? Maybe a callback contains a wrong string or function handle?
Hi Jan,
So after some more fiddling and following your advice I have found the problem. Although I deleted the SelectionChangeFcn handle in the property inspector and the function itself in the .mfile this was the reason for the problem. I then went back to the property inspector and clicked the button on the left of the SelectionChangeFcn which placed a %automatic in the field and then I opened up the .mfile which then also readded the function into the .mfile. Now the problem is not occurring anymore. My assumption was that if I deleted both the handle in the property inspector and the function in the .m file it would allow me to neaten my code up in the .m file and not influence the GUI behaviour. The problem is solved though so thanks.
My experience corroborates this answer. Clicking radio buttons in my GUI also made my program jump to the XXX_OpeningFcn. After putting back the butgroupX_SelectionChangedFcn, that I had removed earlier, this behavior was gone. Clicking the radiobuttons no longer results in the execution of XXX_OpeningFcn. Is this a bug?
@Duijnhouwer: Please post some code which reproduces the behavior you observe.
Sounds like user error to me. Why would you remove the callback that runs when you click on a radio button???

Sign in to comment.

More Answers (1)

Put the function call in the OutputFcn() rather than the OpeningFcn() and see if that helps.

Categories

Find more on Scripts in Help Center and File Exchange

Asked:

TJ
on 4 Jan 2018

Commented:

on 3 Aug 2018

Community Treasure Hunt

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

Start Hunting!