GUI Examples #21:Explore multiple-figure data-passing 2
function [] = GUI_21() % Demonstrate how to get data from one GUI to another (data passing). % Creates a GUI with an editbox and a pushbutton. When the user presses % the pushbutton, another GUI pops up with an editbox. Whatever is in the % editbox of the second GUI when the user hits return will be put into the % edit box of the first GUI. % % Suggested exercise: Alter the code to have the new GUI display whatever % text is in the editbox from the first GUI when the new GUI is created. % Even more advanced: Alter the code so that two new GUIs cannot be % launched simultaneously by pressing the pushbutton repeatedly. % % % Author: Matt Fig % Date: 7/15/2009 S.fh = figure('units','pixels',... 'position',[500 500 200 130],... 'menubar','none',... 'numbertitle','off',... 'name','GUI_21',... 'resize','off'); S.ed = uicontrol('style','edit',... 'units','pix',... 'position',[10 60 180 60],... 'string','Data'); S.pb = uicontrol('style','pushbutton',... 'units','pix',... 'position',[10 20 180 30],... 'string','Push to Get Data',... 'callback',{@pb_call,S}); function [] = pb_call(varargin) % Callback for GUI_21 pushbutton. S = varargin{3}; % Get the structure. f = figure('units','pixels',... 'menubar','none',... 'position',[750 510 200 100]); % Create a new GUI. E = uicontrol('style','edit',... 'units','pixels',... 'position',[10 20 180 60],... 'string','Type something, press return.',... 'callback',{@E_call,varargin{3}}); uicontrol(E); % Allow user to simply hit return without typing anything. % If user closes GUI_21, close new one as well because it will error when % it tries to execute the callback otherwise. set(S.fh,'deletefcn',{@fig_delet,f}) function [] = E_call(varargin) % Callback for secondary GUI editbox. S = varargin{3}; % Get the structure. set(S.ed,'string',get(gcbo,'string')) % Set GUI_21 editbox string. close(gcbf) % Close secondary GUI. function [] = fig_delet(varargin) % Executes when user closes GUI_21. try delete(varargin{3}) catch % Do nothing. end
Popularity: 1% [?]
GUI Examples #20:Explore multiple-figure data-passing, and the stacking order
An editbox is made which will add a title to a selected figure.
function [] = GUI_20() % Demonstrate finding which figure was current before callback execution. % Usage: % % Call GUI_20, then create several plots, for example: % % GUI_20 % x = 0:.1:10; % figure;plot(x,x);figure;plot(x,x.^2);figure;plot(x,x.^3) % % Now click on whichever of the figures needs a title. % Enter the title in the GUI_20 editbox, then push the button. % Clicking on a different figure will make it receive the next title. % GUI_20 can also be called AFTER the figures have been created. % % If no figure exists, one will be created with the title found in the edit % box. % % Suggested exercise: Alter the code so that an xlabel and ylabel % also are added. This could be done by making 2 more edits, and having % the pushbutton use the information from all 3 edits to do the job. % % % Author: Matt Fig % Date: 7/15/2009 S.fh = figure('units','pixels',... 'position',[500 500 350 50],... 'menubar','none',... 'numbertitle','off',... 'name','GUI_20',... 'resize','off'); S.ed = uicontrol('style','edit',... 'units','pixels',... 'position',[10 10 220 30],... 'fontsize',14,... 'string','Enter Title'); S.pb = uicontrol('style','push',... 'units','pixels',... 'position',[240 10 100 30],... 'fonts',14,... 'str','Insert Title',... 'callback',{@pb_call,S}); function [] = pb_call(varargin) % Callback for the button labeled PUSH_1. S = varargin{3}; % Get the structure. CH = get(0,'children'); % Get all figures. if numel(CH)~=1 % If only 1, that is this GUI! AX = get(CH(2),'children'); % Second in list was cf before callback. if ~isempty(AX) T = get(AX,'title'); % Titles are objects! set(T,'string',get(S.ed,'string')) % Set the title. end else % There are other options here. The GUI could popup a message box % saying, "No axes to title!" or something similar. figure % Create a figure. title(get(S.ed,'string')) % This will create an axes too. end
Popularity: 1% [?]
GUI Examples #19:Explore popup selection determination and manipulation
Similar to GUI_18, except that the editbox is allowed to select a value from the popup. The editbox may also add an item to the popup.
function [] = GUI_19() % Demonstrate how to get selection from a popup to an edit box & vis versa. % This is an expansion of GUI_17. Here we will not enforce a specific list % of choices so that any text the user enters into the editbox will be % added to the popup list if it is not already there. % % % Author: Matt Fig % Date: 7/15/2009 S.fh = figure('units','pixels',... 'position',[300 300 300 110],... 'menubar','none',... 'name','GUI_19',... 'numbertitle','off',... 'resize','off'); S.pop = uicontrol('style','pop',... 'units','pixels',... 'position',[20 10 260 40],... 'string',{'one','two','three','four'}); S.ed = uicontrol('style','edit',... 'units','pix',... 'position',[20 60 260 30],... 'fontsize',16,'string','one'); set([S.pop,S.ed],{'callback'},{{@pp_call,S};{@ed_call,S}}); function [] = pp_call(varargin) % Callback for the popup. S = varargin{3}; % Get the structure. P = get(S.pop,{'string','val'}); % Get the users choice. set(S.ed,'string',P{1}{P{2}}); % Assign the user's choice to the edit. function [] = ed_call(varargin) % Callback for the edit. S = varargin{3}; % Get the structure. E = get(S.ed,'string'); % Get the string from the edit. P = get(S.pop,{'string','value'}); % Get the users choice. % Check if edit string is found in pop-up list. tmp = strmatch(E,P{1}); if ~isempty(tmp) set(S.pop,'value',tmp) % Set the pop-up to match the edit. else % We could add the new element to the popup string at either the top or % the bottom. Both methods are shown below. Only uncomment one of % these at a time! set(S.pop,'string',{P{1}{:},E},'value',length(P{1})+1) % Bottom % set(S.pop,'string',{E,P{1}{:}},'value',1) % Top end
Popularity: 1% [?]




















































