2014-05-11 5 views
0

Je voudrais juste savoir s'il serait possible de boucler efficacement les fonctions de l'interface graphique.Fonctions de l'interface graphique en boucle

function Menu1_CreateFcn(hObject, ~, ~) % --- Executes during object creation, after setting all properties. 
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 
     set(hObject,'BackgroundColor','white'); % Set the background color to white 
    end 
    function Menu2_CreateFcn(hObject, ~, ~) % --- Executes during object creation, after setting all properties. 
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 
     set(hObject,'BackgroundColor','white'); % Set the background color to white 
    end 
    function Menu3_CreateFcn(hObject, ~, ~) % --- Executes during object creation, after setting all properties. 
    % if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 
     set(hObject,'BackgroundColor','white'); % Set the background color to white 
    end 
    function Menu4_CreateFcn(hObject, ~, ~) % --- Executes during object creation, after setting all properties. 
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 
     set(hObject,'BackgroundColor','white'); % Set the background color to white 
    end 

Au moment j'ai:

HandleNames = {'Menu1','Menu2','Menu3','Menu4'}; 
for d = 1:4 
    eval('function (HandleNames{d})_Callback(~, ~, ~)'); 
    eval('function (HandleNames{d})_CreateFcn(hObject, ~, ~)'); 
    if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) 
     set(hObject,'BackgroundColor','white'); % Set the background color to white 
    end 
end 

Mais je suis bien conscient que la fonction eval est pas une bonne pratique et il jette quelques erreurs dans la fenêtre de commande, mais fonctionne toujours comme avant. Y aurait-il une façon plus élégante de le faire ou est-ce juste une chose que je dois traiter, Cheers.

Répondre

1

Ah, je vois que vous utilisez le GUIDE. C'est un bon outil pour les GUI simples et jetables, mais une fois que vous essayez de faire quelque chose de bien, vous allez dépasser vos limites. Heureusement, il y a un meilleur moyen. Vous aurez besoin de créer votre interface graphique, en partie ou en totalité, avec les fonctions programmatic GUI. Ainsi, pour la tâche spécifique qui vous intéresse, essayez ceci:

menuSet = {'Hi', 'This is a menu', 'and another', 'neat, huh?'}; 
for menuIndex = 1:numel(menuSet) 
    menuHandle = uimenu(fh,'Label', menuSet{menuIndex); 
    % You can use menuHandle here, to manipulate any of the menus 
    % properties, or add a sub-menu! 
end 

Vous pouvez également ajouter des sous-menus, et attribuer des contextes, et toutes sortes d'autres choses intéressantes. Je sais qu'il existe une courbe d'apprentissage, mais si vous envisagez d'utiliser MATLAB pour toute application graphique sérieuse, je recommande vivement d'apprendre toutes les fonctions de l'interface graphique programmatique, parmi lesquelles uimenu en est une. Bonne chance!

Questions connexes