How to use MATLAB Workspace?

Hello, readers. Welcome to the MATLAB tutorial, where you are learning the basics to advance information about MATLAB without discussing unnecessary concepts. In the last lecture, there was a discussion about variables and their declaration, usage, and types in detail using the MATLAB command prompt. This time, you are learning the workspace window of MATLAB. Usually, people while learning ignores this window because they do not bother about what is happening in it, but the experts know that by applying the simple rules in the workspace window, they can easily get better results. At the end of this lecture, you will be able to answer the following questions:

  • What is the workspace window of MATLAB?

  • How can you save the files from the workshop in different ways?

  • What is the method to load the files of the workspace window?

  • How are characters implemented in MATLAB?

  • What are the details of using the string in the command prompt?

  • What are some of the important functions of MATLAB?

Workspace Window of MATLAB

The workspace window is a type of window that appears in the upper right corner of the MATLAB window. At first, the focus of the user is usually on the command prompt, and hardly anyone observes the presence of the list of variables in the workspace window that they are declaring. So, the user is not required to store the variables separately in this particular window. Keep in mind, the variables in this window can lose their existence when the environment is closed, the same as in other windows. Yet, the reason why I am mentioning this point here is, the variables are saved in this window on their own, and even after we clear the screen of the command prompt, the variables are still shown in the workspace. So, we can say, even if the command prompt does not show the values of the variables, you can find them in the workspace window with all the details of their values and dimensions. 

Now, it is important to show you the above discussion in action with the help of an example. The workspace window has two portions and they are labelled as names and values. Here, we are declaring the variable with the name A in the command prompt and all its data is saved in the table-like format in the workspace.

Notice that when we write clc in the window, we can still see the value of A in the workspace. So, whenever you want the value of A again, you can simply write A in the command prompt and you will get your required value. 

Using Files in Workspace

As we have said earlier, the data in the workspace vanishes once you close MATLAB. So, to save your data, you can easily use it in the future if you want. To do this, you have to follow these steps:

  • Go to the home tab. 

  • Search for the “save workspace” option.

  • Give a meaningful name to the file so that you may remember the name to search for it in the future.

MATLAB will save this file with the extension .mat.

Once you have saved it, you can easily reuse this file whenever you want by merely going into the “Open” option and selecting your desired value, The procedure is the same as the other files in the computer system. 

Saving the Documents Through Command

Another way to do the same task is to use the command prompt to save the file. For this, you just have to write the command in the prompt and I found it more interesting than the first procedure. So, to save the file in the command prompt, you can follow the command given below:

Save MyFile

Once you save your file, the same file with the name chosen by you will be shown in the “current folder” window of your MATLAB. In this way, it becomes super easy to use your required folder again.

How to Load the Files in Workspace

Another command is used to load or open the file saved by you the previous day. So, the command used for this purpose is given as:

Load MyFile

Keep in mind, you have to follow the rule that the name you are specifying must be the name of the file you have same. The spelling and the case size of each and every word must be the same. So, I usually prefer to load the file from the current folder window where I can find all the files saved by me arranged alphabetically.

Usually, people face the issue of an error when they choose the load command on the file saved by them.  Or sometimes, the user gets the error of saving the file in the form of a folder. The issue may be resolved by using the correct current folder from MATLAB. If you are facing the issue as shown in the figure, maybe you are selecting from the wrong folder. The best way to check it is to notice the path of the folder where your files are being saved while saving the file.

Here, before going into other details, I consider it important to tell you about the tabs and the setting of the workspace window. It may be the issue with your MATLAB that you do not get all the required tabs that is shown in the tutorial, so if you want to add more, you can simply do s by going into the settings and choosing the best tabs for you.

Character in the MATLAB

Characters in MATLAB can be used as they are written in normal life. In contrast to most other programming languages, such as C++, you do not have to specify the type of content you want to add to the compiler. You just have to write it between the inverted commas and you are good to go.

In some tutorials, you will also observe a single semicolon at the end of this command. I am not using it because it is up to the choice of the user, so I do not prefer to have additional words in my code. 

A special case comes into observation when we want to add the double quotation in our string message but are not able to do so. Then, the user may use the double quotation mark twice.

This case is important when you are writing long sentences with information about a particular person or thing. 

Concatenation of The String

It is time to discuss an interesting and useful process while using the strings in the array. If you are familiar with the process of concatenation, then you must know that:

“The term "concatenations" means to merge two things together, and in programming languages, it is the process of connecting two strings together in a single sentence.”

You can save the values of the variables and then use them with each other with the help of concatenation.

This helps a lot when you have to use the different types of numbers again and again in different ways. 

Character in the Form of Array in MATLAB

To store and arrange the data in an organized way, arrays are used. If you have great numbers of strings and want to store all the data in the array format, you can do so as you were dealing with the normal numbers before. Yet, this time, you have to put quotation marks before and after the character work or sentence. 

Strings in MATLAB

Strings can be used as characters in MATLAB, but these have two very basic differences that are mentioned below:

Character

String

It has less size than a string.

It is bigger than the character.

It is assigned by using a single quotation mark. 

It is assigned by using the double quotation mark. 


Usually, sentences are used in the strings because they save a large amount of memory, and characters are used in cases where single letters are to be used. Basically, a string in MATLAB is an array of characters. Usually, we can memorize the difference between different data types by keeping in mind, when we deal with the alphabet, we use either strings or characters, and usually, a string is considered the sentence that has to be printed on the screen.

Important Functions In MATLAB

There are different functions of MATLAB that you must know if you are dealing with large data such as strings and characters, For your information, we are discussing some of them.

  • Display function

  • Clear function

  • Maximum Function

  • Minimum function

  • Whos function

The details of both of them are given here:

Display Function

The display function, as the name suggests, is used to display the results, function, or anything that is fed into it. The main use of this function starts when you save any data in the variable or get the result of any calculation and want to describe the result on the screen. No matter if you have other calculations written between the display function and the result you want to show, you can easily get the required output instantly when you call it with the help of the display function. The syntax of this function is given here:

disp(data to display)

Here, the data may be any string, number, variable, or anything that you have saved in the compiler and want to display now. 

Clear Function

We have discussed many times that we can clear the data off the screen, but you have not seen the implementation of this function. This is one of the simplest commands, and I love it because, through this, we can make room on the screen for the new calculations. The syntax is given below:

clc

Yes, it is as simple as shown above. You just have to write this in a new line and your screen will be cleared. One thing that must be cleared up here is that when you use this command, the data is not deleted. It is just cleared from the screen. You can see in the workspace window, all the variables and results are present just in the memory of MATLAB. 

Maximum Function

While dealing with the numbers, you will find this function interesting and useful because it tells you the maximum value of the data that you have provided. It may not look as important as it actually is now, but consider the case when we have a large amount of data, let's say 100, or even 1000, numbers. How will you check what the maximum number is among them? In such cases, the maximum function gives you results. Have a look at the example given below:

I have just tried to give you the simplest example of this function by using some ordinary numbers. Once my data is stored in the variable C, I can now easily get the maximum value among all these values by using a simple command of the maximum function.

Hence, there is no need to check each and every number all the time when I have this function. 

Minimum Function

Another pre-defined function of MATLAB is a minimum function that gives the exact opposite results of the case discussed above so I am not explaining it again and just showing you the implementation. 

Whos function

Here is the last function that I am interested to share with you. This function tells the user about the details of a particular matrix, array, or variable at the command prompt. As in the workshop window, all the values related to that variable will be displayed on the screen.

So, it was the lecture where we defined the little but important concepts of MATLAB. The focus of this lecture was on the workshop window, and we have proved that this simple window is important to us because it shows the summary of all the variables and the saved values at just a glance. So, we hope you have learned a lot from this tutorial. Stay tuned with us for more amazing and easy tutorials.

Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir