Open a new blank document in the MATLAB Editor (in recent versions of MATLAB, do this by selecting the Home tab of the toolstrip, and clicking on New Script). The default keyboard shortcut to create a new script is Ctrl-n
.
Alternatively, typing edit myscriptname.m
will open the file myscriptname.m
for editing, or offer to create the file if it does not exist on the MATLAB path.
In the editor, type the following:
disp('Hello, World!');
Select the Editor tab of the toolstrip, and click Save As. Save the document to a file in the current directory called helloworld.m
. Saving an untitled file will bring up a dialog box to name the file.
In the MATLAB Command Window, type the following:
>> helloworld
You should see the following response in the MATLAB Command Window:
Hello, World!
We see that in the Command Window, we are able to type the names of functions or script files that we have written, or that are shipped with MATLAB, to run them.
Here, we have run the 'helloworld' script. Notice that typing the extension (.m
) is unnecessary. The instructions held in the script file are executed by MATLAB, here printing 'Hello, World!' using the disp
function.
Script files can be written in this way to save a series of commands for later (re)use.