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:

%% My First Script
% Assigns the variables x and y
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

clc

to clear the Command Window.

We can modify the script to end the lines of code in a

;

To suppress the output:

If we type in

clear all

or

clear('all')

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:

%% My Second Script
% Calls up the first script
MyScript1
% Displays x and y in the Command Window
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:

fprintf('My Text')

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

Note how the next prompt >> 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:

fprintf('My Text \n')

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

fprintf('\t My Text \n')

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 %f 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:

fprintf('\t x is %f \n', x)

We can change %f to %0.1f to view the number to 1 decimal place:

x=2;

fprintf('\t x is %0.1f \n', x)

We can change %f to %0.01f to view the number to 2 decimal places:

x=2;

fprintf('\t x is %0.01f \n', x)

We can change %f to %0.001f to view the number to 3 decimal places:

x=2;

fprintf('\t x is %0.001f \n', x)

We can change %f to %d to view the number as a Decimal Notation:

x=2;

fprintf('\t x is %d \n', x)

Now supposing we made the value of the variable x a character opposed to a number. We can change %f to %c to view the character:

x='M';

fprintf('\t x is %c \n', x)

We can change %f to %s to view a string of characters:

x='M';

fprintf('\t x is %s \n', x)

Now supposing we made the value of the variable x a string opposed to a character.

x='MyText';

fprintf('\t x is %s \n', x)

Returning x to a number, this time a large number. We can change %f to %e to view the number using scientific notation:

x=10000;

fprintf('\t x is %e \n', x)

We can change %f to %0.1e to view the number using scientific notation with 1 decimal place:

x=10000;

fprintf('\t x is %0.1e \n', x)

We can change %f to %0.2e to view the number using scientific notation with 2 decimal places:

x=10000;

fprintf('\t x is %0.2e \n', x)

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;

fprintf('\t y is %d and x is %d \n', y, x)

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:

%% My Third Script
% Calls up the first script which has the Variables x and y
MyScript1
% Uses the command fprintf to display the text in the Command Window
fprintf('\n \t the number x is %d and the number y is %f \n',x,y)

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.