Table of Contents
The Idea of a Basic MATLAB Workflow
When you start using MATLAB, you will usually repeat a small set of actions. You start MATLAB, set or check your current folder, create or open a file, run some commands, check the results, make changes, and save your work. This repeated pattern is your basic workflow.
In the very beginning you can work entirely in the Command Window. Later you will move more and more into script files. This chapter focuses on how to interact with MATLAB in this early stage, how to run simple commands, and how to move between typing, checking, and correcting.
Typing and Executing Simple Commands
The most direct way to use MATLAB is to type a command at the prompt in the Command Window and press Enter. MATLAB evaluates the command immediately and shows the result.
For example, you can type a simple calculation:
2 + 3After you press Enter, MATLAB displays
ans =
5
Here, ans is a default variable name that MATLAB uses when you do not assign the result to a variable. If you want to keep the result for later use, you should assign it:
x = 2 + 3
Now MATLAB creates x and displays its value.
You can enter more complicated expressions in exactly the same way, for example
(1 + 2) * 3^2
sin(pi/4)
sqrt(10)Each expression is evaluated as soon as you press Enter.
Suppressing Command Output with Semicolons
By default, MATLAB prints the result of each command in the Command Window. Sometimes you do not want to see the output, especially when you are working with large arrays or you only care about the side effects, such as creating a variable or drawing a figure.
To tell MATLAB not to show the result, end the line with a semicolon:
x = 10; % no output shown
y = x^2 % output shown
z = x^2; % no output shownUsing semicolons to control what appears in the Command Window is an important part of keeping your workflow clean. It becomes even more important when you start writing scripts, because long blocks of code without semicolons produce a lot of text.
Using the Command History and Reusing Commands
When you experiment, you often repeat or slightly modify commands. MATLAB keeps a log of what you have typed, called the command history. You can use this without even opening the Command History window, by using the arrow keys in the Command Window.
Press the up arrow to move backward through previous commands. When you see the command you want, you can run it again by pressing Enter, or you can edit it first. Press the down arrow to move forward again.
For example, if you typed
x = 1:10;
sum(x)
mean(x)
and then you decide you want to compute max(x), you can press the up arrow until you see mean(x), change mean to max, then press Enter.
You can also copy a previous line with the mouse. Select the line in the Command Window, copy, then paste at the prompt, and edit it.
The Command History window, usually docked by default, shows a longer list of past commands. You can double click any entry to run it again directly.
Using Simple Arithmetic and Built In Functions
A large part of basic MATLAB work consists of numeric operations. The most common ones behave exactly like a calculator:
2 + 3 % addition
4 - 1 % subtraction
5 * 6 % multiplication
8 / 2 % division
2^3 % powerMATLAB also offers many built in functions that you can call directly from the Command Window, even before you learn about functions in detail. You call a function by writing its name followed by parentheses that contain its input:
sqrt(9)
log(10)
sin(pi/2)You can combine arithmetic and functions freely in a single command:
result = sin(pi/4) + sqrt(2);The details of arithmetic, operators, and functions are covered later. At this stage it is enough to be comfortable typing expressions and reading their results.
Breaking Long Commands across Multiple Lines
Sometimes a single line becomes long or hard to read. You can split one logical command across several physical lines with three dots .... MATLAB treats the dots at the end of a line as a continuation marker.
For example
x = 1:0.1:10;
y = sin(x) + ...
0.5 * cos(2*x) + ...
0.1 * x;
Here, MATLAB sees the definition of y as a single command. This is useful when you start writing more complex calculations.
Running Groups of Commands with Scripts
Typing many separate commands by hand is slow and makes experiments hard to repeat. As soon as you find yourself repeating more than a few commands, you should use a script file.
A script is a text file that contains a list of MATLAB commands, one after another, in the order you want MATLAB to execute them. You create or open scripts in the Editor, then run them so MATLAB executes all lines in sequence, as if you had typed them into the Command Window.
For example, you might create a script with the following content:
x = 0:0.1:2*pi;
y = sin(x);
plot(x, y)
When you run the script, MATLAB does three things: creates x, computes y, and draws the plot.
Scripts fit naturally into the workflow: you try a few commands interactively, then move them into a script to repeat them. You make changes in the script, save it, run it again, and observe the results.
How to create and run script files in detail is covered later. In this chapter focus on the idea that multiple commands can be saved as a script to be run together.
Editing and Rerunning Code
Most of your time in MATLAB will be spent in a cycle of running code, checking results, and adjusting your commands. Even if you only work in the Command Window, you can already use this cycle.
You might start with
x = 0:10;
y = x^2
MATLAB shows an error, because x^2 is not valid for vectors in the way you intended. You then correct the command to
y = x.^2and run it again. This quick feedback loop is central to MATLAB work.
Once you use scripts, the loop looks similar. You open the script in the Editor, make a change, save the file, run it, and inspect the output in the Command Window, in figures, or in the Workspace. Then you repeat.
Using the Current Folder in Your Workflow
The current folder affects where MATLAB looks for script files and where it saves files by default. A simple workflow is to create one folder per small project, make that the current folder, and keep your scripts and data files there.
To change the current folder, use the folder browser or type a command. For example, on Windows you might enter
cd 'C:\Users\YourName\Documents\MATLAB\myproject'When the folder is set, running a script is as simple as typing its name in the Command Window:
myscript
If the script file myscript.m is in the current folder, MATLAB finds it and executes it.
You can also check which directory you are in by typing
pwd
This command prints the path of the current folder. Using cd and pwd is an important part of managing where your work lives.
Combining Commands with Simple Output Display
Often you want to see values with a bit more context than just the number. For interactive exploration, you can use disp and fprintf to show information in the Command Window.
For example
x = 5;
disp(x)
simply prints the value of x without the variable name. To print text and a value together, you can use
x = 5;
fprintf('The value of x is %d\n', x)This displays
The value of x is 5These simple display commands are part of a basic workflow, especially inside scripts, where you may want to report intermediate results in a human friendly way.
Handling Errors in Simple Commands
While learning, you will see many error messages. An error means MATLAB tried to execute your command but could not. The message tells you what went wrong and often where.
For example, if you type
x = [1 2 3;
4 5];MATLAB may complain about inconsistent dimensions. When this happens, read the error text and check the command you typed. Often the mistake is a missing bracket, parenthesis, or semicolon, or a mismatch in sizes.
The key point for your workflow is not to be discouraged by errors. Type a command, see what happens, adjust, and try again. The interactive nature of MATLAB makes this process quick.
Details on debugging and more advanced error handling come later. At this stage, you only need to be comfortable with the idea that errors are normal and part of experimental work.
Cleaning Up the Command Window and Variables
During an interactive session, the Command Window can fill up quickly and the Workspace can contain many variables. Sometimes you want to clean things before running a new set of commands.
To clear the text from the Command Window, type
clcThis does not remove any variables. To remove all variables from the Workspace, use
clear
After clear, any variables you had defined disappear and will no longer be available. If you only want to remove one or two variables, you can give their names:
clear x yClearing the screen and variables is a common step before re running a script, especially when you want to be sure that old values do not affect the new run.
Saving and Continuing Your Work Session
In a typical day you will not finish everything in one session. A simple workflow is to keep your script files saved in your project folder and then close MATLAB. When you open MATLAB again and set the same current folder, your files are still there. You can open them in the Editor and continue.
If you want to preserve the current workspace variables as they are, you can save them to a MAT file with
save mysessionLater, you can restore them with
load mysessionUsing scripts, saving your work regularly, and keeping files in organized folders are basic habits that make MATLAB sessions reproducible and easier to resume.
Putting the Workflow Together
A very simple MATLAB workflow for a beginner might look like this.
Start MATLAB and check or set the current folder. Try a few simple commands in the Command Window, such as numeric expressions or small vector operations. When you find a set of commands you like, move them into a new script file in the Editor. Save the script in your current folder, run it, and inspect the results in the Command Window and any generated figures. If you see an error or an unexpected result, return to the Editor or to the Command Window, change the relevant command, and run again. Use semicolons to control output, clc and clear to tidy your session when needed, and save and load if you want to keep your current set of variables.
As you repeat these steps, the cycle of writing, running, observing, and adjusting becomes natural.
Important points to remember:
Use the Command Window for quick, interactive commands and calculations.
End a line with ; to run a command without printing its result.
Reuse and edit previous commands with the up and down arrow keys.
Group related commands into a script file so you can run them together.
Keep your current folder set to the place where your scripts and data live.
Use clc to clear the Command Window and clear to remove variables from the Workspace.
Save scripts often so you can reproduce and continue your work in later sessions.