2015-08-17 9 views
5

Come trasferire oggetti GUI (pulsanti, cursori, elenchi, ecc.) Avanti e indietro tra 2 cifre, mantenendo la loro funzionalità (callback e interazioni)? In altre parole, trasferisci tutti gli oggetti dalla figura 1 alla figura 2 e chiedi loro di eseguire i loro script come nella figura 1.Oggetti della GUI di ri-parenting

+2

Utilizzare la proprietà 'Parent' di uicontrols:' s et (your_object_Handle, 'Parent', destination_figure_handle) '. Se hanno bisogno di prendere i dati dalla nuova figura per eseguire lo script/la funzione, dovrai fare attenzione a scegliere i dati giusti. – Hoki

risposta

1

Il trucco qui è di gestire il modo in cui si impostano i controlli e i callback e di utilizzare l'interruttore legacy in copyobj come "un"documented here

l'esempio seguente consente di Reparent un copia di tutti gli oggetti di figura di capire - mi rendo conto che non è "trasferimento", ma la copia -> ma tutti funzionano ancora indipendentemente ...

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 
Problemi correlati