2017-06-21 54 views
0

是否可以根据输入参数创建GUI? 例如,我想用my_gui(n)调用一个GUI,GUI出现时带有一个在另一个下面的样式'按钮'的n个ui控件,每个按钮都有一个单独的回调。而n可以是从1到20的任何数字。 这是以某种方式使用eval的吗? 还是有人有一个想法如何做到这一点?根据输入参数创建GUI

感谢您的努力

拉斐尔

回答

1

当然其可能的,例如:

function myGui(n) 
    if nargin == 0; n = randi(20); end 
    if n > 20 || n < 1 
    error ('myGui:n', 'The input parameter "n" (%i) is outwith the allowed range (0 to 20)', n); 
    end 
    % create the parent figure 
    hFig = figure; 
    % create the positions 
    locations = linspace (0.9, 0.1, n); 
    % loop for n to create them, in this example the callback displays the number of the button pushed. 
    % The buttons have a fixed height of 0.05 (normalized). 
    for ii=1:n 
    uicontrol ('parent', hFig, 'style', 'push', 'Units', 'normalized', 'Position', [0.1 locations(ii) 0.5 0.05], 'String', num2str(ii), 'Callback', @(a,b)fprintf ('Pushed %i\n', ii)); 
    end 
end 
+0

哇。这是快速和准确的。非常感谢,这很好 – Rafael