我只想知道是否有可能高效地循环GUI函数。
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现在我有:
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但我清楚地知道,eval函数不是很好的实践,它在命令窗口中抛出了一些错误,但仍然像以前一样运行。是有更优雅的方法来做这件事,还是这只是我必须要处理的事情,干杯。
发布于 2014-05-11 12:33:21
啊,我看你在用向导。这是一个很好的工具,简单,扔掉GUI,但一旦你尝试做任何整洁的事情,你会推动它超越它的限制。幸运的是还有更好的方法。您需要使用编程GUI函数部分或全部构建GUI。因此,对于您感兴趣的特定任务,请尝试如下:
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您还可以添加子菜单,分配上下文,以及所有其他有趣的东西。我知道有一个学习曲线,但是如果您计划在任何严肃的GUI应用程序中使用MATLAB,我强烈建议学习所有编程GUI函数,其中uimenu只是其中之一。祝好运!
https://stackoverflow.com/questions/23592379
复制相似问题