Overriding GUI Objects

How can I pass GUI objects (buttons, sliders, lists, etc.) back and forth between two digits, while maintaining my functionality (callbacks and interactions)? In other words, transfer all the objects from Figure 1 to Figure 2 and ask them to execute their scripts, as shown in Figure 1.

+5
source share
1 answer

The trick here is how to manage the settings of uicontrols and your callbacks and use the deprecated switch in copyobj as "un" registered here

In the example below, you can track a copy of all your objects from the drawing to the figure - I understand that this is not a β€œtransfer”, but a copy β†’ but they all still work independently ...

function test_reparentObjects % Create a new figure f1 = figure ( 'Name', 'Original Figure' ); % Create some objects - make sure they ALL have UNIQUE tags - this is important!! axes ( 'parent', f1, 'tag', 'ax' ); uicontrol ( 'style', 'pushbutton', 'position', [0 0 100 25], 'string', 'plot', 'parent', f1, 'callback', {@pb_cb}, 'tag', 'pb' ); uicontrol ( 'style', 'pushbutton', 'position', [100 0 100 25], 'string', 'reparent', 'parent', f1, 'callback', {@reparent}, 'tag', 'reparent' ); uicontrol ( 'style', 'text', 'position', [300 0 100 25], 'string', 'no peaks', 'parent', f1, 'tag', 'txt' ); uicontrol ( 'style', 'edit', 'position', [400 0 100 25], 'string', '50', 'parent', f1, 'callback', {@pb_cb}, 'tag', 'edit' ); end function pb_cb(obj,event) % This is a callback for the plot button being pushed % find the parent figure h = ancestor ( obj, 'figure' ); % from the figure find the axes and the edit box ax = findobj ( h, 'tag', 'ax' ); edt = findobj ( h, 'tag', 'edit' ); % convert the string to a double nPeaks = str2double ( get ( edt, 'string' ) ); % do the plotting cla(ax) [X,Y,Z] = peaks(nPeaks); surf(X,Y,Z); end function reparent(obj,event) % This is a callback for reparenting all the objects currentParent = ancestor ( obj, 'figure' ); % copy all the objects -> using the "legacy" switch (r2014b onwards) % this ensures that all callbacks are retained children = copyobj ( currentParent.Children, currentParent, 'legacy' ); % create a new figure f = figure ( 'Name', sprintf ( 'Copy of %s', currentParent.Name ) ); % reparent all the objects that have been copied for ii=1:length(children) children(ii).Parent = f; end end 
+1
source

All Articles