0

is it possible to create a GUI depending on its input arguments? For example I'd like to call a GUI with my_gui(n) and the GUI appears with n ui-controls of style 'pushbutton' one below the other and each pushbutton has an separate callback. Whereas n can be any nomber from 1 to 20. Is this somehow possilbe using eval? Or does anyone have an idea how to do this?

Thanks for your effort

Rafael

1 Answer 1

1

Of course its possible, for example:

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
Sign up to request clarification or add additional context in comments.

1 Comment

Wow. This was fast and accurate. Thanks alot, this works great

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.