Help with store data in GUI when calling a function
5 views (last 30 days)
Show older comments
Hi,
I have a figure include two radio buttons and a push button. In this figure, I store all data in handles structure named 'handles'.
When I choose one of two radio buttons, either shows a new figure by calling a function named 'figure2' or a message in command window with contents 'No figure showed'.
I want to store data in 'figure2' in 'handles' structure which includes both data of figure1 and figure2. In my code, I just stored data 'figure2' in 'handles' structure. May I know how to do it?
Here is my code:
function untitled1_build
clc
clear all
close all
% --- FIGURE -------------------------------------
handles.figure1 = figure( ...
'Tag', 'figure1', ...
'Units', 'characters', ...
'Position', [102.8 41.0769230769231 76 16.1538461538462], ...
'Name', 'untitled1', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Color', [0.941 0.941 0.941]);
% --- PANELS -------------------------------------
handles.uipanel3 = uibuttongroup( ...
'Parent', handles.figure1, ...
'Tag', 'uipanel3', ...
'Units', 'characters', ...
'Position', [9.8 6 48.2 8.53846153846154], ...
'Title', {'Button Group'});
% --- PUSHBUTTONS -------------------------------------
handles.pushbutton2 = uicontrol( ...
'Parent', handles.figure1, ...
'Tag', 'pushbutton2', ...
'Style', 'pushbutton', ...
'Units', 'characters', ...
'Position', [45.8 1.38461538461538 20.2 2.38461538461538], ...
'String', {'Push Button'});
% --- RADIO BUTTONS -------------------------------------
handles.radiobutton2 = uicontrol( ...
'Parent', handles.uipanel3, ...
'Tag', 'radiobutton2', ...
'Style', 'radiobutton', ...
'Units', 'characters', ...
'Position', [25.8 2.23076923076923 17.4 2.38461538461538], ...
'String', {'Radio Button'});
handles.radiobutton1 = uicontrol( ...
'Parent', handles.uipanel3, ...
'Tag', 'radiobutton1', ...
'Style', 'radiobutton', ...
'Units', 'characters', ...
'Position', [2.4 2.23076923076923 17.4 2.38461538461538], ...
'String', {'Radio Button'});
set(handles.uipanel3 ,'selectedobject',[])
set(handles.pushbutton2,'callback',{@pb_callback,handles})
% Push button function
function pb_callback(varargin)
handles=varargin{3}
a=findobj(get(handles.uipanel3,'selectedobject'));
switch a
case handles.radiobutton1
figure2
case handles.radiobutton2
disp('No figure showed')
end
% Figure2
function figure2
% --- FIGURE -------------------------------------
%handles=varargin{3}
handles.figurenew = figure( ...
'Tag', 'figurenew', ...
'Units', 'pixels', ...
'Position', [515 585 380 160], ...
'Name', 'Figure2', ...
'MenuBar', 'none', ...
'NumberTitle', 'off', ...
'Color', [0.941 0.941 0.941]);
% --- STATIC TEXTS -------------------------------------
handles.text2 = uicontrol( ...
'Parent', handles.figurenew, ...
'Tag', 'text2', ...
'Style', 'text', ...
'Units', 'characters', ...
'Position', [7.6 5.30769230769231 14.2 1.61538461538462], ...
'FontSize', 10, ...
'String', 'Value Sk', ...
'HorizontalAlignment', 'left');
handles.text3 = uicontrol( ...
'Parent', handles.figurenew, ...
'Tag', 'text3', ...
'Style', 'text', ...
'Units', 'pixels', ...
'Position', [270 71 30 21], ...
'FontSize', 10, ...
'String', 'm2', ...
'HorizontalAlignment', 'left');
% --- PUSHBUTTONS -------------------------------------
handles.pushbutton1 = uicontrol( ...
'Parent', handles.figurenew, ...
'Tag', 'pushbutton1', ...
'Style', 'pushbutton', ...
'Units', 'pixels', ...
'Position', [260 20 81 30], ...
'FontSize', 10, ...
'String', 'Next');
% --- EDIT TEXTS -------------------------------------
handles.edit3 = uicontrol( ...
'Parent', handles.figurenew, ...
'Tag', 'edit3', ...
'Style', 'edit', ...
'Units', 'characters', ...
'Position', [35.8 5.46153846153846 14.2 1.61538461538462], ...
'FontSize', 10, ...
'BackgroundColor', [1 1 1])
Thanks and please excuse for my English.
4 Comments
Image Analyst
on 10 Nov 2014
You have to first place a button group panel, and then place individual radio buttons into that group box. Then they will operate as expected where selecting one deselects all the others. Then you put all your code into the callback for the button group rather than into the callbacks for each individual radio button. For example, in this GUI tutorial/template MAGIC there is this callback.
% --- Executes when selected object is changed in grpRadButtonGroup.
function grpRadButtonGroup_SelectionChangeFcn(hObject, eventdata, handles)
% hObject handle to the selected object in grpRadButtonGroup
% eventdata structure with the following fields (see UIBUTTONGROUP)
% EventName: string 'SelectionChanged' (read only)
% OldValue: handle of the previously selected object or empty if none was selected
% NewValue: handle of the currently selected object
% handles structure with handles and user data (see GUIDATA)
switch get(eventdata.NewValue,'Tag') % Get Tag of selected object.
case 'radOption1'
% Code for when radiobutton1 is selected.
txtInfo = sprintf('Option 1 is selected and the others are deselected.\n\nResults will be sorted in order of decreasing area.');
set(handles.pnlResults, 'title', 'Results sorted by area');
case 'radOption2'
% Code for when radiobutton2 is selected.
txtInfo = sprintf('Option 2 is selected and the others are deselected.\n\nResults will be sorted in order of decreasing intensity.');
set(handles.pnlResults, 'title', 'Results sorted by intensity');
case 'radOption3'
% Code for when togglebutton1 is selected.
txtInfo = sprintf('Option 3 is selected and the others are deselected.\n\nResults will be sorted in order of decreasing ECD.');
set(handles.pnlResults, 'title', 'Results sorted by ECD');
% case 'togglebutton2'
% Code for when togglebutton2 is selected.
% Continue with more cases as necessary.
otherwise
% Code for when there is no match.
end
set(handles.txtInfo, 'String', txtInfo);
return; % from grpRadButtonGroup_SelectionChangeFcn
Download MAGIC for an easy way to get started with a generic GUI template.
Answers (1)
Image Analyst
on 10 Nov 2014
This is rapidly getting out of hand. Just a few simple controls and now you have hundreds of lines where you have to tediously set all kinds of properties and callbacks. It will just get worse and worse as you develop your app and make it fancier and more sophisticated. Why not just use GUIDE? http://blogs.mathworks.com/videos/category/gui-or-guide/
See Also
Categories
Find more on Interactive Control and Callbacks 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!