MATLAB Programming/Workspace

From Wikibooks, open books for an open world
Jump to navigation Jump to search

MATLAB Workspace[edit | edit source]

MATLAB has a programming environment in its own and needs an area to store the variables and other program data that is stored while using the tool. MATLAB has a command window as shown, and a base workspace which is the default workspace. Functions in MATLAB also have their own workspace, and the visibility of a variable is dependent on the workspace that it resides in. If the variable is in the base workspace then it is visible to all of the scripts and functions in MATLAB, while a function's local variables are visible to that function only and are not usable outside that function.

Type this on the MATLAB Command Window

   My_Var = 10

You will see

   My_Var =
   10

as a response, also note that there would also be an addition to the Base workspace.

My_Var in Base workspace

To check out what all variables are stored in the MATLAB workspace, we can use the following command.

   who

This would list out all the variables currently present in the MATLAB workspace, but would not give much idea about the data types and the size of the variables, to have those in the result use this command instead

   whos

By this time, i think there would be much clutter on your command window.

To clear out all the output on the command window, use this command.

   clc

This WOULD NOT clear out the variables and the history. so that you would have a new clean and fresh command window.

The output of whos on my screen showed something like this Output of "whos" command in Matlab

Now, if you notice the Size is marked as 1X1, but why? The reason for this is that in MATLAB all the data is realized as double precision arrays (well, its the way MATLAB is designed to work as it is a scientific computing tool). And so scalars like these are realized as a 1X1 array. Simple!

After all this if you think your work is done and you do not need all those variables in the MATLAB workspace now, you can relieve yourself of your Programmers baggage by using this command.

   clear

This clears out any and all variables in the workspace. But if your miseries are not yet to finish and would like to remove a variable or two.

   clear <var1> <var2>....

where var1, var2, var3,.... are the variables that you want to clear, do not put the three dots in the end, that's just for description.