📖 Scenario: You are creating a simple user interface (UI) in MATLAB to greet users. The UI will have a button and a text label. When the button is clicked, the text label will change to say "Hello, User!".
🎯 Goal: Build a MATLAB UI with a button and a text label. When the button is clicked, the label updates to greet the user.
📋 What You'll Learn
Create a figure window named fig
Add a button named btn with the label "Click Me"
Add a text label named txt with initial text "Waiting..."
Write a callback function named buttonCallback that changes the text label to "Hello, User!" when the button is clicked
Assign the callback function to the button's ButtonPushedFcn property
💡 Why This Matters
🌍 Real World
Creating simple graphical user interfaces helps users interact with programs easily without typing commands.
💼 Career
UI programming skills are useful for engineers and scientists who build tools for data visualization and user interaction.
Progress0 / 4 steps
1
Create the main figure window
Create a figure window and assign it to a variable called fig using uifigure.
MATLAB
Hint
Use fig = uifigure(); to create the window.
2
Add a button and a text label
Add a button to fig named btn with the text "Click Me" using uibutton. Also add a text label named txt with the initial text "Waiting..." using uilabel.
MATLAB
Hint
Use uibutton(fig, 'Text', 'Click Me') and uilabel(fig, 'Text', 'Waiting...').
3
Write the button callback function
Write a function named buttonCallback that takes two inputs (src and event) and changes the txt.Text property to "Hello, User!".
MATLAB
Hint
Define buttonCallback with inputs src and event. Change txt.Text inside.
4
Connect the button to the callback and run
Set the button's ButtonPushedFcn property to the function handle @buttonCallback. Then run the program and observe the output when clicking the button.
MATLAB
Hint
Use btn.ButtonPushedFcn = @buttonCallback; to link the button to the function.
Practice
(1/5)
1. Which MATLAB function is used to create a new interactive window for UI components?
easy
A. window
B. uifigure
C. uiwindow
D. figure
Solution
Step 1: Understand the purpose of UI windows
MATLAB uses a special function to create windows that support interactive UI components like buttons and sliders.
Step 2: Identify the correct function for UI windows
The function uifigure creates a window designed for UI components, unlike figure which is for plots.
Final Answer:
uifigure -> Option B
Quick Check:
UI window = uifigure [OK]
Hint: Use uifigure for UI windows, not figure [OK]
Common Mistakes:
Confusing uifigure with figure
Using non-existent functions like uiwindow
Assuming window creates UI windows
2. Which of the following is the correct syntax to add a button to a UI figure named fig?
easy
A. btn = uibutton();
B. btn = button(fig);
C. btn = addbutton(fig);
D. btn = uibutton(fig);
Solution
Step 1: Recall the function to create buttons in UI figures
MATLAB uses uibutton to create buttons inside a UI figure.
Step 2: Check the syntax for specifying the parent UI figure
The parent figure must be passed as an argument to uibutton, so uibutton(fig); is correct.
Final Answer:
btn = uibutton(fig); -> Option D
Quick Check:
Button creation = uibutton(fig) [OK]
Hint: Always pass the figure handle to uibutton [OK]
Common Mistakes:
Using button() instead of uibutton()
Not passing the figure as argument
Using non-existent addbutton() function
3. What will be the output when running this code?