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: #ff00ff;">;</span>

To suppress the output:

If we type in

<span style="color: #0000ff;">clear</span> <span style="color: #ff9900;">all</span>

or

<span style="color: #0000ff;">clear(</span><span style="color: #800080;">'</span><span style="color: #ff9900;">all</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

We remove the variables we created:

Now running the script:

Recreates the Variables

x

and

y

but there is no Output in the Command Window

One can also run a Script by typing its name in the Command Window. In this case we are going to create a Second Script:

<strong><span style="color: #008000;">%% My Second</span> </strong><span style="color: #008000;"><strong>Script</strong>
</span>
<span style="color: #008000;">% Calls up the</span> <span style="color: #008000;">first script
</span>
MyScript1
<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:

<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span>My Text<span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

Displays the string of text My Text in the Command Window.

Note how the next prompt

&gt;&gt;

appears immediately after My Text>>, this is because we never told it to start on a new line. We can add

\n

 within the text to add a new line:

<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span>My Text <span style="color: #ff0000;">\n</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

If we wanted a tab to indent the text we can add one using

\t
<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #ff0000;">\t</span> My Text <span style="color: #800000;">\n</span><span style="color: #800080;">'</span><span style="color: #0000ff;">)</span>

Supposing we want to read the value of a variable and print it in the Command Window. First we will need to create the value.

x=2;

Next we will to specify the format of the variable that we wish to display amongst the text we want to print for instance 

<span style="color: #ff0000;">%f</span>

 to specify a fixed point notation number and then we will need to input the value we wish to show as a second input argument in the function:

<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%f</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

We can change 

<span style="color: #ff0000;">%f</span>

 to 

<span style="color: #ff0000;">%0.1f</span>

to view the number to 1 decimal place:

x=2;
<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%0.1f</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

We can change 

<span style="color: #ff0000;">%f</span>

 to 

<span style="color: #ff0000;">%0.01f</span>

to view the number to 2 decimal places:

x=2;
<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%0.01f</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

We can change 

<span style="color: #ff0000;">%f</span>

 to 

<span style="color: #ff0000;">%0.001f</span>

to view the number to 3 decimal places:

x=2;
<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%0.001f</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

We can change 

<span style="color: #ff0000;">%f</span>

 to 

<span style="color: #ff0000;">%d</span>

to view the number as a Decimal Notation:

x=2;
<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%d</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

Now supposing we made the value of the variable

x

a character opposed to a number. We can change 

<span style="color: #ff0000;">%f</span>

 to 

<span style="color: #ff0000;">%c</span>

to view the character:

x=<span style="color: #800080;">'</span>M<span style="color: #800080;">'</span>;
<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%c</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

We can change 

<span style="color: #ff0000;">%f</span>

 to 

<span style="color: #ff0000;">%s</span>

to view a string of characters:

x=<span style="color: #800080;">'</span>M<span style="color: #800080;">'</span>;
<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%s</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

Now supposing we made the value of the variable

x

a string opposed to a character.

x=<span style="color: #800080;">'</span>MyText<span style="color: #800080;">'</span>;
<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%s</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

Returning 

x

to a number, this time a large number. We can change 

<span style="color: #ff0000;">%f</span>

 to 

<span style="color: #ff0000;">%e</span>

to view the number using scientific notation:

x=10000;

<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%e</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

We can change 

<span style="color: #ff0000;">%f</span>

 to 

<span style="color: #ff0000;">%0.1e</span>

to view the number using scientific notation with 1 decimal place:

x=10000;

<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%0.1e</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

We can change 

<span style="color: #ff0000;">%f</span>

 to 

<span style="color: #ff0000;">%0.2e</span>

to view the number using scientific notation with 2 decimal places:

x=10000;

<span style="color: #0000ff;">fprintf(</span><span style="color: #800080;">'</span><span style="color: #800000;">\t</span> x is <span style="color: #ff0000;">%0.2e</span> <span style="color: #800000;">\n'</span><span style="color: #808080;">,</span> <span style="color: #ff0000;">x</span><span style="color: #0000ff;">)</span>

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.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.