2017-06-21 51 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