Search the courses, subjects, tracks, games and features, or jump straight to a page.
Learn it your way
A short class with Sarah and her students. The other ways are under More.
Sign up free to listen. Transcripts are free to read.
Today we’re discussing how to assign values to variables in MATLAB scripts. Can anyone tell me how a variable can get its value?
You can define them directly in the script!
Good point! That's the first method. What about other ways?
You can assign them from the command prompt before running the script.
Exactly! And today we'll primarily focus on the third method, which is using the input command within the script itself.
When you use the input command, how do you prompt the user for data?
You can add a string inside the input function to display a message, like 'Enter your value here.'
That's correct! This makes your script interactive. Who can give me an example of this?
We can ask for scores in a game and calculate the average based on the scores entered!
Perfect! Let’s work through that example together.
Let’s create a script that calculates the average score from three games. What would our code start with?
We would use the input command for each game's score.
Exactly! What else do we need to do after we get all scores?
We need to calculate the average.
Right! Let’s finish the script by displaying the average. Now, can anyone summarize what we've learned?
We learned how to use input to get user scores and calculate averages!
Ways to assign values to variables in scripts include defining in-script, command prompt assignment, and user input.
The input command allows scripts to receive direct input from the user during execution.
A centralized collection of MATLAB commands executed together.
When you want a score from the game, use input and it's never lame!
Imagine a teacher who asks students how many points they scored in their game. Each student shares their scores, and the teacher calculates the average — but only after everyone has inputted their scores!
I.P.S. for input in scripts: I for Input command, P for Prompt user, S for Store the value.
I.S.U. — Input Script User helps remember how to get data from a user in scripts.
Term
What is the purpose of the input command?
Click to flip
Term
How do you define a variable in a MATLAB script?
Click to flip
This section describes how variables in a MATLAB script can be assigned values, focusing particularly on user input through the script.
The section explores three methods to assign values to variables in a MATLAB script. It emphasizes using the input command to acquire values directly from the user when a script is executed, showcasing practical examples.
When executing a MATLAB script, it's important that the variables used for calculations have assigned values. This section details three methods for assigning values to these variables:
input command, users can be prompted to provide values when the script is executed.The following demonstrates user input:
% This script calculates the average points scored in three games.
game1 = input('Enter the points scored in the first game: ');
game2 = input('Enter the points scored in the second game: ');
game3 = input('Enter the points scored in the third game: ');
average = (game1 + game2 + game3) / 3
When this is run in MATLAB, the user will be prompted to input scores for three games, leading to a calculated average displayed at the end. This enhances interactivity and flexibility when running scripts.