Bird
Raised Fist0
MATLABdata~20 mins

Callback functions in MATLAB - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Callback Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this MATLAB callback example?

Consider the following MATLAB code that sets a callback for a button press. What will be printed when the button is pressed?

MATLAB
function testCallback
  btn = uicontrol('Style','pushbutton','String','Click me', ...
    'Position',[100 100 100 40], 'Callback', @buttonPressed);

  function buttonPressed(src, event)
    disp('Button was clicked!')
  end
end
AError: Undefined function or variable 'buttonPressed'.
BButton was clicked!
CNo output is printed.
DButton was clicked! Button was clicked!
Attempts:
2 left
💡 Hint

Look at what the callback function buttonPressed does when the button is clicked.

🧠 Conceptual
intermediate
1:30remaining
Which statement about MATLAB callback functions is true?

Choose the correct statement about callback functions in MATLAB GUI programming.

ACallback functions can be function handles, anonymous functions, or nested functions.
BCallback functions must always be nested functions inside the main function.
CCallback functions cannot accept any input arguments.
DCallback functions execute immediately when the GUI is created.
Attempts:
2 left
💡 Hint

Think about the different ways MATLAB allows you to specify callbacks.

🔧 Debug
advanced
2:30remaining
Why does this MATLAB callback cause an error?

Examine the code below. When the button is clicked, it causes an error. What is the cause?

MATLAB
function faultyCallback
  btn = uicontrol('Style','pushbutton','String','Click', ...
    'Position',[100 100 100 40], 'Callback', @buttonPressed);
end

function buttonPressed(src, event)
  disp('Clicked!')
end
AThe callback function must be anonymous to work properly.
BThe callback function must not have any input arguments.
CThe callback function <code>buttonPressed</code> is not in the same file or nested, so MATLAB cannot find it.
DThe button style 'pushbutton' is deprecated and causes errors.
Attempts:
2 left
💡 Hint

Consider MATLAB's function visibility rules for callbacks.

📝 Syntax
advanced
2:00remaining
Which option correctly assigns a callback that passes extra arguments?

You want to assign a callback to a button that calls myCallback with extra arguments arg1 and arg2. Which option is correct?

Abtn.Callback = @(src,event) myCallback(src, event, arg1, arg2);
Bbtn.Callback = @myCallback(arg1, arg2);
Cbtn.Callback = myCallback(arg1, arg2);
Dbtn.Callback = @(src) myCallback(arg1, arg2);
Attempts:
2 left
💡 Hint

Think about how to use anonymous functions to pass extra arguments in callbacks.

🚀 Application
expert
3:00remaining
What is the value of variable count after this callback sequence?

Consider this MATLAB GUI code that counts button clicks using a persistent variable inside the callback. What is the value of count after pressing the button 3 times?

MATLAB
function clickCounter
  btn = uicontrol('Style','pushbutton','String','Count clicks', ...
    'Position',[100 100 120 40], 'Callback', @countClicks);

  function countClicks(~,~)
    persistent count
    if isempty(count)
      count = 0;
    end
    count = count + 1;
    disp(['Clicked ' num2str(count) ' times']);
  end
end
AError: persistent variable not initialized
B1
C0
D3
Attempts:
2 left
💡 Hint

Remember how persistent variables keep their value between function calls.

Practice

(1/5)
1. What is the main purpose of a callback function in MATLAB GUI programming?
easy
A. To speed up the program execution by parallel processing
B. To store data permanently in a file
C. To execute code in response to user actions or events
D. To create new variables automatically

Solution

  1. Step 1: Understand the role of callback functions

    Callback functions are designed to respond when a user interacts with a GUI element, like clicking a button.
  2. Step 2: Identify the correct purpose

    Among the options, only executing code in response to user actions matches the callback function's role.
  3. Final Answer:

    To execute code in response to user actions or events -> Option C
  4. Quick Check:

    Callback = respond to events [OK]
Hint: Callbacks run when user interacts with GUI elements [OK]
Common Mistakes:
  • Thinking callbacks store data permanently
  • Confusing callbacks with parallel processing
  • Assuming callbacks create variables automatically
2. Which of the following is the correct way to assign a callback function named myCallback to a push button btn in MATLAB?
easy
A. btn.Callback = myCallback;
B. btn.Callback = &myCallback;
C. btn.Callback = 'myCallback';
D. btn.Callback = @myCallback;

Solution

  1. Step 1: Recall MATLAB callback syntax

    In MATLAB, the @ symbol is used to create a function handle for callbacks.
  2. Step 2: Identify the correct assignment

    Assigning @myCallback correctly creates a handle to the function for the callback.
  3. Final Answer:

    btn.Callback = @myCallback; -> Option D
  4. Quick Check:

    Use @ for function handles [OK]
Hint: Use @ before function name for callbacks [OK]
Common Mistakes:
  • Omitting @ symbol for function handle
  • Using quotes instead of @ for function handle
  • Using & which is not valid in MATLAB
3. Consider the following MATLAB code snippet for a button callback:
function myCallback(src, event)
    disp('Button pressed');
end

btn.Callback = @myCallback;

% When the button is clicked, what will be displayed?
medium
A. Button pressed
B. src event
C. Error: Undefined function
D. No output

Solution

  1. Step 1: Understand the callback function behavior

    The function myCallback displays the text 'Button pressed' when called.
  2. Step 2: Recognize the callback assignment

    The button's callback is set to @myCallback, so clicking the button triggers the function and prints the message.
  3. Final Answer:

    Button pressed -> Option A
  4. Quick Check:

    Callback prints message on click [OK]
Hint: Callback prints message when triggered [OK]
Common Mistakes:
  • Expecting variable names to print
  • Thinking callback causes error without GUI
  • Assuming no output from disp function
4. What is wrong with this MATLAB callback assignment?
btn.Callback = myCallback;
Assuming myCallback is a function defined elsewhere.
medium
A. Missing @ symbol to create function handle
B. Callback property does not exist for buttons
C. Function name should be a string, not a variable
D. Callback functions cannot be assigned directly

Solution

  1. Step 1: Check callback assignment syntax

    In MATLAB, callbacks require a function handle using the @ symbol.
  2. Step 2: Identify the missing element

    The code assigns the function name without @, so MATLAB treats it as a variable, causing an error.
  3. Final Answer:

    Missing @ symbol to create function handle -> Option A
  4. Quick Check:

    Callbacks need @ before function name [OK]
Hint: Always use @ before function name for callbacks [OK]
Common Mistakes:
  • Omitting @ symbol
  • Confusing property names
  • Thinking function name as string is correct
5. You want to create a MATLAB GUI button that changes its own label to 'Clicked!' when pressed. Which callback function correctly achieves this?
hard
A. function changeLabel() btn.String = 'Clicked!'; end
B. function changeLabel(src, ~) src.String = 'Clicked!'; end
C. function changeLabel(~, event) event.String = 'Clicked!'; end
D. function changeLabel(src) src.Label = 'Clicked!'; end

Solution

  1. Step 1: Understand callback inputs

    The first input src is the source object (the button), which can be used to change its properties.
  2. Step 2: Identify correct property and syntax

    The button's label text is stored in the String property, so setting src.String = 'Clicked!'; updates the label.
  3. Final Answer:

    function changeLabel(src, ~) src.String = 'Clicked!'; end -> Option B
  4. Quick Check:

    Use src.String to change button label [OK]
Hint: Use src.String to update button text inside callback [OK]
Common Mistakes:
  • Using event instead of src to change properties
  • Using wrong property name like Label
  • Not including required input arguments
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard