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 are going to learn how to enter multiple statements in MATLAB. Can anyone tell me why this might be useful?
It could save time when you want to calculate a lot at once!
Exactly! By using commas or semicolons, we can input multiple statements. Does anyone know the difference between using a comma and a semicolon?
Commas show the output of each statement, while semicolons prevent output.
That's correct! Now let's see an example together. Here's a line of code: a=7; b=cos(a), c=cosh(a).
I see! The outputs for b and c will be displayed because of the comma.
Well done, everyone! So, using semicolons helps keep our workspace clean while using commas allows us to see outputs directly. Let's summarize: when entering multiple statements, use semicolons to suppress output and commas to display results.
Now, let's try entering multiple statements together. What happens if we only use semicolons in our example x=5; y=sin(x); z=exp(x)?
We wouldn't see any of the calculations in the output, right?
Exactly! All results would be stored but not displayed. Remember, we can always check by typing just y or z later. Why might this be beneficial?
It keeps the command window uncluttered when working with lots of calculations!
Precisely! Now, let's write a few lines of code on our own!
Sometimes while entering multiple statements, errors can occur. Have any of you experienced an error in MATLAB?
I got an error once when I forgot to add a semicolon.
That's a common mistake! If you forget to add a semicolon and then write the next statement, MATLAB can get confused. Let's see an example: x=10 y=5; without the semicolon in between.
Right, it gave me an 'Unexpected MATLAB expression' error!
Exactly! Always double check your statements. Good catch! Remember, using the up-arrow key can help recall prior commands for corrections.
If you want screens to stay pristine, use a semicolon to keep outputs unseen.
Imagine juggling multiple balls; with semicolons, you keep the crowd from seeing the drops while you master your act.
Remember 'C for Comma - Output, S for Semicolon - Silent' to differentiate their effects.
COSS - Commas Output; Semicolons Silent.
Term
What does a semicolon do in MATLAB?
Click to flip
Term
What do commas do when entered into MATLAB?
Click to flip
This section explains how to enter multiple statements on a single line in MATLAB using commas or semicolons.
In MATLAB, users can streamline their code by entering multiple statements on a single line. Commas separate statements that display outputs, while semicolons can suppress output, allowing for cleaner code. This feature simplifies coding and enhances efficiency.
In MATLAB, it is possible to enhance productivity by entering multiple statements in a single line. This is achieved using either commas (,) or semicolons (;). The use of commas allows for multiple statements without suppressing their output, while semicolons enable the user to execute multiple commands without displaying the output of each command, thus keeping the command window organized and clean.
For instance, by writing a=7; b=cos(a), c=cosh(a), the commands for defining variables and calculating their values occur seamlessly in one line. The output will display results for b and c.
This practice not only saves time but also makes the code more readable when correctly applied. This approach is particularly useful in operations requiring multiple calculations or assignments in series.