The world’s leading publication for data science, AI, and ML professionals.

How to learn Matlab

All you need to know to begin your Matlab journey

Photo by Rich Tervet on Unsplash
Photo by Rich Tervet on Unsplash

Matlab has arguably the most comprehensive documentation compared to other open-source programming languages. Because of that, I always imagine that Learning and getting into the Matlab world should be a quite straightforward process, no matter you’ve already been a seasoned programmer in other languages, or this is even the first one you would like to start with.

However, when I started this journey recently. I surprisingly found reading the official documentation sometimes is not the most effective way to get a taste of this language. Exactly just because of its terrific comprehensiveness, it should be better described as a reference instead of a tutorial. In light of that, I decided to summarize what I’ve learned, which I believe will cover the most essential things you need to know to begin with, into this blog.

The codes used in the blog can be found in my Github repository.


Preface: My recommended steps for learning a new language

Although it is just my own opinion, I strongly believe that the first step for learning a language should always be getting familiar with its data types. The least important step is to memorize a variety of functions. None of us **** can know of every single function, but thanks to the documentation and our "best friend" – Google, getting directed to the right function has become a much easier thing nowadays. However, if you don’t understand what each variable’s properties are, that will be a hard way to move forward and develop new knowledge on top of that.

In this blog, I will strictly follow this procedure and show you how I usually learn a new language:

Step1: Learning Matlab Data Types

One thing I found useful to understand Matlab is,

Every object is a matrix (array)

Let’s use the following code snippet to illustrate the point:

DataType Bootcamp (Image by Author)
DataType Bootcamp (Image by Author)

I tend to use the dumbest examples to explain the basic idea without any function or logical syntax. So in the Matlab console, I typed more than 10 different variables, and their data types are shown in the workspace window, shown on the right. As you can see, everything in Matlab seems to behave like a matrix/array (size column in the workspace window), when you typed a=1 , a actually, become a 1*1 array, with the class as double , that is because double is the default class for all the numerical variables. You can feel free to cast it to another class, use corresponding casting functions. If you want to know how to do that, just google that, that’s why I suggested that function should be the last step to learn, instead of the first thing to worry about. For now, you just need to know it is possible to cast different classes, in terms of how you can figure that out later.

Moving on, we initiated another variable b='hello' , you may think it is a string, but actually, b happens to be an array of size 1*5 , with the class as char . If you really want to have a string variable, you need to use a double quotation, so we have c="hello" . Now c become an 1*1 array, with the class as string now. char array treats each character as a unit but string treats that whole string/sentence as a whole. You can have string array as well like variable g=["hello","yes"] .

Then, we have a numeric vector and matrix. In Matlab, use whitespace for horizontal expansion and use ; for vertical expansion. So, for a 2*3 matrix f , it should be created using [1 2 3; 4 5 6] . Likewise, a row vector d should be [1 2 3] and a column vector e should be [1;2;3] .

What if we have mixed classes in an array? We need to use cell array to store mixed types. Variable h is a cell array of the shape 2*3 , its class is cell , indicating the types are different for each element in the array, as you can observe, we have both char, double and even nested double array.

The next class type is struct , it resembles dictionary in Python and represents a key-value relationship, we have variable i to illustrate the creation of struct .Again, i is an array as well, with the class type as struct .

Till now, I hope the idea is a bit clearer, you can think about the common objects in Matlab all as arrays, an array can contain other arrays as well. However, the class types are different. To reinforce the understanding, let’s build a variable j , which I make a 3-dimension array, but the class type will be struct . We can simply specify the shape of this array as (1,2,2), and assign a struct element into one slot.

What are the total built-in class types that Matlab has? As with most other languages, commonly used types include int , which can further consist of int8 , int16 … and unsigned ones like uint8 etc. Then double for floating numbers, char and string for texts, logical which contains values either true or false , along with struct class type that we explained above.

Last but not least, have you ever noticed that there were always a ; at the end of each command? That’s for suppressing this line being printed in the console. By default, every line will be executed and printed in the console. Also, every command that does not have an assigned variable (see line 13 a+1), will be assigned to an internal variable ans , that’ why we have a ans object in the workspace window with value as 2 (ans = a + 1, and a = 1).

That concludes the introduction of basic data type. Since everything is an array, it will also be helpful to know how to access each element in the array:

How to access the elements in the array? (Image by Author)
How to access the elements in the array? (Image by Author)

Three things to remember are:

  1. use round brackets for the normal array and curly brackets for the cell array
  2. Matlab is 1-based indexing (Python is 0-based indexing)
  3. understand end keyword and slice operation (forward and backward)

In addition to above class type, another interesting class type is called "table", let’s say the following example:

table class type in Matlab (Image by Author)
table class type in Matlab (Image by Author)

We created a table shown as in the upper right corner, and in the lower left, we can see its class type is table , with the shape of 33. If we use the row vector for each column (name, gpa, grade), then the shape would become 1 3. To access each column, we need to use the dot .symbol. The "table" class type can be really helpful in the common tasks because it resembles the dataframe object in other interpreted languages.

Step2: Loop, If, custom function, etc

I found out quite a lot of tutorials focusing a lot on explaining how to write for loop and if statement in each language. While they are important, but if you’ve already had experience in at least one language, a simple cheatsheet would be sufficient. Since this tutorial really aims to illustrate how to learn a new language yourself instead of just instill pieces of knowledge. I strongly suggest you just google it, and you will get what you need in one second, but for completeness, let me summarize them for you in the following screenshot:

Cheatsheet: essential logical statements in Matlab (Image by Author)
Cheatsheet: essential logical statements in Matlab (Image by Author)

The only thing worth noting is that, when constructing a function, Matlab tend to have a unique way of defining that, the formula looks like function return_value = function_name(arg1,arg2,...) , then you just call the function by its function_name.

Step3: more advanced features and a variety of built-in functions

Now it is time to explore more advanced topics, for instance, how to build a custom class (object-oriented programming, OOP) in Matlab? Or, what is an anonymous function in Matlab? You can get what you need by just googling them (I already did that for you if you just click the link), I hope that can help you to get used to how to delve into the real Programming world since this is literally how we do in a day-to-day basis.

As I said initially, I didn’t even teach you one single built-in function in my tutorial, for instance, how to concatenate two strings and conduct dot product or cross product. Nonetheless, having a good command of basic data types will make your future learning way much easier. That concludes what I wish to share with you today, I hope this small article can help you grasp other more advanced operations you need for your specific tasks.


That’s about it! I hope you find this article interesting and useful, thanks for reading! If you like this article, follow me on medium, thank you so much for your support. Connect me on my Twitter or LinkedIn, also please let me know if you have any questions or what kind of tutorials you would like to see In the future!


Related Articles