Domanda

I would just like to know if it would be possible to efficiently loop GUI functions.

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

At the moment I have:

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

But I am well aware that the eval function is not good practice and it is throwing some errors in the command window but is still functioning like before. Would there be a more elegant way to do this or is it just a thing that I have to deal with, Cheers.

È stato utile?

Soluzione

Ah, I see you're using GUIDE. It's a nice tool for simple, throw-away GUIs, but once you try to do anything neat, you're going to push it past it's limits. Fortunately, there's a better way. You'll need to build your GUI, in part or in whole, with the programmatic GUI functions. So, for the specific task you're interested in, try this:

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

You can also add sub-menus, and assign contexts, and all sort of other interesting things. I know there's a learning curve, but if you plan on using MATLAB for any serious GUI application, I highly recommend learning all of the programmatic GUI functions, of which uimenu is just one. Good luck!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top