Create Scripts

Scripts are the simplest kind of program file because they have no input or output arguments. They are useful for automating series of MATLAB® commands, such as computations that you have to perform repeatedly from the command line or series of commands you have to reference.

You can open a new script in the following ways:

  • Highlight commands from the Command History, right-click, and select Create Script.

  • Click the New Script button on the Home tab.

  • Use the edit function.

    This code opens the file file_name:

    edit file_name
    If file_name is unspecified, MATLAB opens a new file called Untitled.

For example, suppose you save this code that generates random numbers between 0 and 100 as a script called numGenerator.m:

columns = 10000;
rows = 1;
bins = columns/100;

list = 100*rand(rows,columns);

You can run the code in numGenerator.m using either of these methods:

  • Type the script name numGenerator on the command line and press Enter

  • Click the Run button on the Editor tab

When execution completes, the variables (columns, rows, bins and list) remain in the MATLAB workspace. To see a listing of variables, enter whos at the command prompt. Scripts share the base workspace with your interactive MATLAB session and with other scripts.

More About

Was this topic helpful?