Callback functions in MATLAB - Time & Space Complexity
We want to understand how the time taken by callback functions changes as the input grows.
How does the number of times a callback runs affect the total work done?
Analyze the time complexity of the following code snippet.
function applyCallback(arr, cb)
n = length(arr);
for i = 1:n
cb(arr(i));
end
end
% Example callback function
function printElement(x)
disp(x);
end
This code runs a callback function once for each element in an array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that calls the callback for each element.
- How many times: Exactly once per element, so n times for an array of size n.
As the array gets bigger, the callback runs more times, growing the work linearly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 calls to callback |
| 100 | 100 calls to callback |
| 1000 | 1000 calls to callback |
Pattern observation: Doubling the input doubles the number of callback calls.
Time Complexity: O(n)
This means the total time grows directly in proportion to the number of elements.
[X] Wrong: "The callback runs only once, so time is constant regardless of input size."
[OK] Correct: The callback runs once for each element, so more elements mean more calls and more time.
Understanding how callbacks scale helps you explain how event-driven or functional code performs as data grows.
What if the callback itself contains a loop over the entire array? How would the time complexity change?
Practice
Solution
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.Step 2: Identify the correct purpose
Among the options, only executing code in response to user actions matches the callback function's role.Final Answer:
To execute code in response to user actions or events -> Option CQuick Check:
Callback = respond to events [OK]
- Thinking callbacks store data permanently
- Confusing callbacks with parallel processing
- Assuming callbacks create variables automatically
myCallback to a push button btn in MATLAB?Solution
Step 1: Recall MATLAB callback syntax
In MATLAB, the @ symbol is used to create a function handle for callbacks.Step 2: Identify the correct assignment
Assigning@myCallbackcorrectly creates a handle to the function for the callback.Final Answer:
btn.Callback = @myCallback; -> Option DQuick Check:
Use @ for function handles [OK]
- Omitting @ symbol for function handle
- Using quotes instead of @ for function handle
- Using & which is not valid in MATLAB
function myCallback(src, event)
disp('Button pressed');
end
btn.Callback = @myCallback;
% When the button is clicked, what will be displayed?Solution
Step 1: Understand the callback function behavior
The functionmyCallbackdisplays the text 'Button pressed' when called.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.Final Answer:
Button pressed -> Option AQuick Check:
Callback prints message on click [OK]
- Expecting variable names to print
- Thinking callback causes error without GUI
- Assuming no output from disp function
btn.Callback = myCallback;Assuming
myCallback is a function defined elsewhere.Solution
Step 1: Check callback assignment syntax
In MATLAB, callbacks require a function handle using the @ symbol.Step 2: Identify the missing element
The code assigns the function name without @, so MATLAB treats it as a variable, causing an error.Final Answer:
Missing @ symbol to create function handle -> Option AQuick Check:
Callbacks need @ before function name [OK]
- Omitting @ symbol
- Confusing property names
- Thinking function name as string is correct
Solution
Step 1: Understand callback inputs
The first inputsrcis the source object (the button), which can be used to change its properties.Step 2: Identify correct property and syntax
The button's label text is stored in theStringproperty, so settingsrc.String = 'Clicked!';updates the label.Final Answer:
function changeLabel(src, ~) src.String = 'Clicked!'; end -> Option BQuick Check:
Use src.String to change button label [OK]
- Using event instead of src to change properties
- Using wrong property name like Label
- Not including required input arguments
Ready to prove it?
Pass the test and this pattern moves unknown
