Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 107131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T01:42:21+00:00 2026-05-11T01:42:21+00:00

Is it possible to write a GUI from inside a function? The problem is

  • 0

Is it possible to write a GUI from inside a function?

The problem is that the callback of all GUI-functions are evaluated in the global workspace. But functions have their own workspace and can not access variables in the global workspace. Is it possible to make the GUI-functions use the workspace of the function? For example:

function myvar = myfunc()     myvar = true;     h_fig = figure;      % create a useless button     uicontrol( h_fig, 'style', 'pushbutton', ...                       'string', 'clickme', ...                       'callback', 'myvar = false' );      % wait for the button to be pressed     while myvar         pause( 0.2 );     end      close( h_fig );      disp( 'this will never be displayed' ); end 

This event-loop will run indefinitely, since the callback will not modify myvar in the function. Instead it will create a new myvar in the global workspace.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T01:42:21+00:00Added an answer on May 11, 2026 at 1:42 am

    There are a number of ways to build a GUI, such as using the App Designer, GUIDE, or creating it programmatically (I’ll illustrate this option below). It’s also important to be aware of the different ways to define callback functions for your GUI components and the options available for sharing data between components.

    The approach I’m partial to is using nested functions as callbacks. Here’s a simple GUI as an example:

    function make_useless_button()    % Initialize variables and graphics:   iCounter = 0;   hFigure = figure;   hButton = uicontrol('Style', 'pushbutton', 'Parent', hFigure, ...                       'String', 'Blah', 'Callback', @increment);    % Nested callback function:   function increment(~, ~)     iCounter = iCounter+1;     disp(iCounter);   end  end 

    When you run this code, the counter displayed should increment by one every time you press the button, because the nested function increment has access to the workspace of make_useless_button and thus can modify iCounter. Note that the button callback is set to a function handle to increment, and that this function must accept two arguments by default: a graphics handle for the UI component that triggered the callback, and a structure of associated event data. We ignore them with the ~ in this case since we aren’t using them.

    Extending the above approach to your particular problem, you could add your loop and change the callback so it sets your flag variable to false:

    function make_stop_button()    % Initialize variables and graphics:   keepLooping = true;   hFigure = figure;   hButton = uicontrol('Style', 'pushbutton', 'Parent', hFigure, ...                       'String', 'Stop', 'Callback', @stop_fcn);    % Keep looping until the button is pressed:   while keepLooping,     drawnow;   end    % Delete the figure:   delete(hFigure);    % Nested callback function:   function stop_fcn(~, ~)     keepLooping = false;   end  end 

    The drawnow is needed here to give the button callback a chance to interrupt the program flow within the loop and modify the value of keepLooping.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 223k
  • Answers 223k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer 57/30 performs integer division. To obtain a float (or double)… May 13, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer There is a lot of confusion associated with encodings in… May 13, 2026 at 12:40 am
  • Editorial Team
    Editorial Team added an answer Try this: var rooms = from roomBinding in DALManager.Context.RoomBindings group… May 13, 2026 at 12:40 am

Related Questions

I have been asked to write a GUI that will allow objects to be
My application often fetch data from a webpage using WebRequest, but it isn't possible
I am writing a GUI wrapper for windows api right now ( i can't
I'm not entirely sure this is possible. I have a legacy GUI application written

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.