function handles_nested() % Utilisation des fonctions imbriquése pour partager les données avec la % fonction principale close all fh = figure('Units', 'Normalized', 'Position',[0.6 0.6 0.15 0.15], 'MenuBar', 'none'); set(0, 'DefaultUicontrolUnits', 'normalized') ; e1 = uicontrol(fh, 'style', 'EDIT', 'Posit',[0.5 0.7 0.3 0.2], 'str', 22); e2 = uicontrol(fh, 'style', 'Edit', 'Posit',[0.5 0.4 0.3 0.2], 'str', '44'); e3 = uicontrol(fh, 'style', 'EDIT', 'Posit',[0.5 0.1 0.3 0.2]); b1 = uicontrol(fh, 'style', 'pushbutton', 'position',[0.1 0.4 0.3 0.2],... 'str', 'Alpha', 'call', @alphaa); b2 = uicontrol(fh, 'style', 'pushbutton', 'position',[0.1 0.1 0.3 0.2],... 'str', 'Num', 'call', @numm); function numm(obj,event) n1 = str2num(get(e1, 'string')); n2 = str2num(get(e2, 'string')); set(e3, 'string',n1+n2); end function alphaa(obj,event) s1 = get(e1, 'string'); s2 = get(e2, 'string'); set(e3, 'string',[s1 s2]); end end |
function handle_global() global e1 e2 e3 close all fh = figure('Units', 'Normalized', 'Position',[0.6 0.6 0.15 0.15],... 'MenuBar', 'none'); set(0, 'DefaultUicontrolUnits', 'normalized') ; e1 = uicontrol(fh, 'style', 'EDIT', 'Posit',[0.5 0.7 0.3 0.2], 'str', 22); e2 = uicontrol(fh, 'style', 'Edit', 'Posit',[0.5 0.4 0.3 0.2], 'str', '44'); e3 = uicontrol(fh, 'style', 'EDIT', 'Posit',[0.5 0.1 0.3 0.2]); b1 = uicontrol(fh, 'style', 'pushbutton', 'position',[0.1 0.4 0.3 0.2],... 'str', 'Alpha', 'call', @alphaa); b2 = uicontrol(fh, 'style', 'pushbutton', 'position',[0.1 0.1 0.3 0.2],... 'str', 'Num', 'call', @numm); end function numm(obj,event) global e1 e2 e3 n1 = str2num(get(e1, 'string')); n2 = str2num(get(e2, 'string')); set(e3, 'string',n1+n2); end function alphaa(obj,event) global e1 e2 e3 s1 = get(e1, 'string'); s2 = get(e2, 'string'); set(e3, 'string',[s1 s2]); end |
function handle_param() % passage des handles comme paramètres au moment de l'appel des fonctions close all fh = figure('Units', 'Normalized', 'Position',[0.6 0.6 0.15 0.15],... 'MenuBar', 'none'); set(0, 'DefaultUicontrolUnits', 'normalized') ; e1 = uicontrol(fh, 'style', 'EDIT', 'Posit',[0.5 0.7 0.3 0.2], 'str', 22); e2 = uicontrol(fh, 'style', 'Edit', 'Posit',[0.5 0.4 0.3 0.2], 'str','44'); e3 = uicontrol(fh, 'style', 'EDIT', 'Posit',[0.5 0.1 0.3 0.2]); b1 = uicontrol(fh, 'style', 'pushbutton', 'position',[0.1 0.4 0.3 0.2],... 'str', 'Alpha', 'callback', {@alphaa e1 e2 e3}); b2 = uicontrol(fh, 'style', 'pushbutton', 'position',[0.1 0.1 0.3 0.2],... 'str', 'Num', 'callback', {@numm e1 e2 e3}); end function numm(obj,event, e1, e2, e3) n1 = str2num(get(e1, 'string')); n2 = str2num(get(e2, 'string')); set(e3, 'string',n1+n2); end function alphaa(obj,event, e1, e2, e3) s1 = get(e1, 'string'); s2 = get(e2, 'string'); set(e3, 'string',[s1 s2]); end |
function handle_param_struct() % les handle sont groupés dans une structure qui est passée en % paramètre au fonctions appelées close all fh = figure('Units', 'Normalized', 'Position',[0.6 0.6 0.15 0.15],... 'MenuBar', 'none'); set(0, 'DefaultUicontrolUnits', 'normalized') ; hs.e1 = uicontrol(fh, 'style', 'EDIT', 'Posit',[0.5 0.7 0.3 0.2], 'str', 22); hs.e2 = uicontrol(fh, 'style', 'Edit', 'Posit',[0.5 0.4 0.3 0.2], 'str', '44'); hs.e3 = uicontrol(fh, 'style', 'EDIT', 'Posit',[0.5 0.1 0.3 0.2]); b1 = uicontrol(fh, 'style', 'pushbutton', 'position',[0.1 0.4 0.3 0.2],... 'str', 'Alpha', 'call', {@alphaa hs}); b2 = uicontrol(fh, 'style', 'pushbutton', 'position',[0.1 0.1 0.3 0.2],... 'str', 'Num', 'call', {@numm hs}); end function numm(obj,event, ids) n1 = str2num(get(ids.e1, 'string')); n2 = str2num(get(ids.e2, 'string')); set(ids.e3, 'string',n1+n2); end function alphaa(obj,event, ids) s1 = get(ids.e1, 'string'); s2 = get(ids.e2, 'string'); set(ids.e3, 'string',[s1 s2]); end |
function handle_guidata() close all fh = figure('Units', 'Normalized', 'Position',[0.6 0.6 0.15 0.15], 'MenuBar', 'none'); set(0, 'DefaultUicontrolUnits', 'normalized') ; hs.e1 = uicontrol(fh, 'style', 'EDIT', 'Posit',[0.5 0.7 0.3 0.2], 'str', 22); hs.e2 = uicontrol(fh, 'style', 'Edit', 'Posit',[0.5 0.4 0.3 0.2], 'str', '44'); hs.e3 = uicontrol(fh, 'style', 'EDIT', 'Posit',[0.5 0.1 0.3 0.2]); b1 = uicontrol(fh, 'style', 'pushbutton', 'position',[0.1 0.4 0.3 0.2],... 'str', 'Alpha', 'call', @alphaa); b2 = uicontrol(fh, 'style', 'pushbutton', 'position',[0.1 0.1 0.3 0.2],... 'str', 'Num', 'call', @numm); guidata(fh, hs); end function numm(obj,event) ids = guidata(obj); n1 = str2num(get(ids.e1, 'string')); n2 = str2num(get(ids.e2, 'string')); set(ids.e3, 'string',n1+n2); end function alphaa(obj,event) hndl = guidata(obj); s1 = get(hndl.e1, 'string'); s2 = get(hndl.e2, 'string'); set(hndl.e3, 'string',[s1 s2]); end |