Kahibaro
Discord Login Register

Command Window, Workspace, and Current Folder

Overview

In everyday MATLAB use, three parts of the Desktop work together very closely: the Command Window, the Workspace, and the Current Folder. Understanding how they interact is essential for running commands, keeping track of your variables, and working with your files.

This chapter focuses only on these three areas and how to use them together in practice.

The Command Window

The Command Window is where you type MATLAB commands and see their immediate results. It is the main text interface to the MATLAB language.

You enter commands at the prompt, which typically looks like:

text
>>

When you press Enter, MATLAB evaluates the command and prints any output in the same window. For instance, typing:

matlab
2 + 3

returns:

matlab
ans =
     5

If you do not assign the result to a variable, MATLAB uses a default variable named ans. For absolute beginners it is helpful to assign values to your own variables explicitly, such as:

matlab
x = 2 + 3

The statement now stores the value in x, and you will see the result printed but you also keep it for later use.

You can recall previous commands using the up and down arrow keys. This saves time and reduces typing. You can edit the recalled command directly in the Command Window before pressing Enter again.

If you want a command to run without displaying its result, end it with a semicolon ;:

matlab
x = 2 + 3;

In this case MATLAB stores the value in x but does not print it in the Command Window.

When a command is taking too long or you entered something by mistake, you can interrupt it by pressing Ctrl + C in the Command Window. MATLAB stops the current execution and returns to the prompt.

The Workspace

The Workspace is the collection of variables that are currently stored in memory in your MATLAB session. The Workspace is not about files on disk, but about values that exist while MATLAB is running.

The Workspace Browser is a panel that usually appears on the Desktop and lists the names of all variables, such as x, y, data, and so on, along with basic information like size and type. When you execute commands in the Command Window, new variables appear in the Workspace list. When you clear variables, they disappear from this list.

For example, if you type:

matlab
a = 10;
b = rand(1,3);

you will see a and b appear in the Workspace Browser. MATLAB stores a as a scalar and b as a 1 by 3 array. The Workspace Browser updates automatically whenever commands create, change, or remove variables.

You can view the value of a variable by simply typing its name in the Command Window:

matlab
b

MATLAB then displays its contents.

It is important to remember that the Workspace is specific to your current MATLAB session. If you close MATLAB, the Workspace is cleared automatically. To keep variables for later, you must save them to a file, which is covered in a later chapter.

Current Folder

The Current Folder represents the folder on your computer that MATLAB is currently using as its main working location for files. It is usually displayed in a panel called Current Folder and also in the address bar near the top of the Desktop.

When you run scripts, functions, or load and save files, MATLAB looks in the Current Folder if you do not specify any other path. This makes the Current Folder a central concept for organizing and locating your MATLAB files.

You can change the Current Folder in several ways. The simplest way is to use the address bar. Click in the bar, type a new folder path, and press Enter. Alternatively, you can navigate through the folder tree in the Current Folder panel, and double click a folder to move into it.

You can also change the Current Folder with a command. For example, if you have a folder called myproject on your desktop, on Windows you might type:

matlab
cd('C:\Users\YourName\Desktop\myproject')

The command cd changes the Current Folder. On macOS or Linux the path would look different, for example:

matlab
cd('/Users/YourName/Desktop/myproject')

Once you change the Current Folder, the Current Folder panel updates to show the contents of the new location, such as scripts, data files, and subfolders.

How They Work Together

The Command Window, Workspace, and Current Folder are closely connected in everyday work.

When you run commands in the Command Window, you create and modify variables, and these are reflected in the Workspace. For example, typing:

matlab
x = 1:5;
y = sum(x);

creates variables x and y that you can see in the Workspace Browser. If you then run a script file located in the Current Folder by typing its name in the Command Window, that script can create additional variables that also appear in the Workspace.

The Current Folder determines which script or function files you can run simply by typing their name. If the file myscript.m is in the Current Folder, you can run it by typing:

matlab
myscript

in the Command Window. If the file is not in the Current Folder or on the search path, MATLAB does not find it and reports an error.

Reading and writing files from the Command Window also uses the Current Folder by default. Suppose there is a data file data.mat in the Current Folder. The command:

matlab
load('data.mat')

loads variables from that file into the Workspace. They then appear in the Workspace Browser, and you can use them in the Command Window.

Conversely, if you create some variables in the Workspace and want to save them, the command:

matlab
save('results.mat')

creates a file called results.mat in the Current Folder, containing the Workspace variables. The specifics of saving and loading are discussed in later chapters, but for now it is enough to see that the Current Folder is where MATLAB looks for and places these files by default.

Practical Tips for Beginners

At the start of a working session, check that the Current Folder is set to the location of your project or exercises. Make sure that any scripts or data files you want to use are visible in the Current Folder panel.

Use the Workspace Browser to keep track of which variables you have created and to avoid confusion over names. If you are not sure what values a variable contains, type its name in the Command Window to display it.

If you see unexpected results, sometimes it helps to clear the Workspace and start fresh so that old variables do not interfere. How to clear variables and manage the Workspace more systematically will be described in a dedicated chapter.

Finally, remember that the Command Window is your immediate testing area. You can try out small commands there, observe how they affect the Workspace, and see how files in the Current Folder are used in practice.

Important points to remember:

  1. The Command Window is where you type commands and see immediate results.
  2. The Workspace holds all variables currently in memory during your MATLAB session.
  3. The Current Folder is the default location where MATLAB looks for scripts, functions, and data files.
  4. Commands in the Command Window create and modify variables in the Workspace.
  5. Scripts and files in the Current Folder can be run or accessed directly by their names.

Views: 5

Comments

Please login to add a comment.

Don't have an account? Register now!