by Dr Philip Yip (Dell Community Rockstar and Microsoft Windows Insider MVP)
An Introduction to Scripts and Printing Text to the Command Window
Tutorial Video
Creating a Script
We can create a new Script by right clicking in the Current Folder and selecting New → Script:
We will then be asked for a Script Name:
Script Names follow the rules of Variable Names. Opening the Script will Display the Script in the Editor:
Here you can type in Lines of Code, note each Line is numbered to the Left Hand Side:
Typing in:
%%
begins a section break. The text will become Green and Bold. Sections are useful for debugging code as you can run each section individually if desired. The text itself is ignored by MATLAB but useful for the user/end user.
%
begins a comment. The text will become Green. The text itself is ignored by MATLAB but useful for the user/end user.
I will type in the following:
<span style="color: #008000;">%% My First</span> <span style="color: #008000;">Script</span>
<span style="color: #008000;">
</span>
<span style="color: #008000;">% Assigns the variables</span> <span style="color: #008000;">x and y
</span>
x=2
y=4
Note the changes to MyScript1 are unsaved. We can see this because the name MyScript1.m* ends with a *
We can save the changes using the save button or [Ctrl] + [s]:
Notice the absence of the *
To run the script we can right click it in the Current Folder and select Run:
If the Script is open in the Editor, we will also get a Run button on the Toolbar:
Running the script executes the lines of code. This creates the two variables x and y and displays their output to the Command Window:
If we type in
Now we type in
<span style="color: #0000ff;">clc</span>
to clear the Command Window.
We can modify the script to end the lines of code in a
<span style="color: #008000;">% Displays x and y in the</span> <span style="color: #008000;">Command Window
</span>
x
y
If we once again clear all the Variables and clear the Command Window. We can run MyScript2:
On Line 3 it will Call Up MyScript1 and create
x
and
y
which will allow it to print these values to the Command Window:
If we once again clear all the Variables and clear the Command Window. Then if we rename MyScript1:
Then if we try and launch MyScript2 (which has a dependence on MyScript1, which can no longer be found) we will get the following error message:
Commenting out the code for MyScript1, we can do this by the addition of a % or by pressing [Ctrl] + [r] when the line is selected. To uncomment out the line we can remove the % or press [Ctrl] + [t] while the line is selected:
Trying to run the script again fails because there is no Variable x (or Variable y), these were created in the other Script MyScript1:
Care should be taken when using Nested Scripts to make sure they are all available otherwise it can quickly lead to error messages like those shown above.
We will now look at printing text to the Command Window using the function:
If we want to reference more than one variable, say
x
and
y
in a text. We will to specify the format of each variable amongst the text and then we will need to list the variables as additional input arguments:
x=2;
y=4;
<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> y is <span style="color: #ff0000;">%d</span> and x is <span style="color: #ff9900;">%d</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">y<span style="color: #808080;">, <span style="color: #ff9900;">x</span></span></span><span style="color: #0000ff;">)</span>
We can write a new script MyScript3, calling up the MyScript1 which creates the Variables x and y so that MyScript3 can print them within text using fprintf:
<span style="color: #008000;">%% My Third</span> <span style="color: #008000;">Script
</span>
<span style="color: #008000;">% Calls up the first script which has the Variables x and y
</span>
MyScript1
<span style="color: #008000;">% Uses the command fprintf to display the text</span> <span style="color: #008000;">in the Command Window
</span>
<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\n</span> <span style="color: #800000;">\t</span> the number x is <span style="color: #ff9900;">%d</span> and the number y is <span style="color: #ff0000;">%f</span> <span style="color: #800000;">\n</span><span style="color: #800080;">'</span><span style="color: #808080;">,</span><span style="color: #ff9900;">x</span><span style="color: #808080;">,</span><span style="color: #ff0000;">y</span><span style="color: #0000ff;">)</span>
Some characters such as
'
have a meaning to MATLAB. Note what happens when I try to add the text
Philip's
the
'
ends the text input.
However a second
'
continues it:
This means
''
has to be used in the place of
'
Likewise
%%
in place of
%
Notice also now that there are two sections. The currently selected Section is in yellow.