Summary After 10 Years of Using Matlab: Key Points to Master

Reading Directory

  • 1. Introduction

  • 2. Brief Introduction to Matlab

  • 3. Introduction to Matlab Development Environment

  • 4. Common Commands

  • 5. Matrix Operations That Must Be Mentioned

  • 6. Programming Syntax

  • 7. My Thoughts on Matlab

I can’t remember the last time I wrote more than 20 lines of Matlab code; it was probably in 2013 when I wrote two articles that involved some Matlab content. The last time I wrote a Matlab program over 200 lines was back in 2011. Recently, to help a reader with an issue on my blog, I dug out my Matlab code from 2011 and felt a lot. It’s hard to put into words, but I can understand the familiar code and even modify it. It feels like I haven’t used it for many years, and I’ve forgotten even the basic functions. Nevertheless, when I encounter problems, I can still write and modify code. This shows that programming is not just a matter of memory and experience; more importantly, it involves learning methods and problem-solving techniques. Therefore, I decided to write this article to share my understanding of Matlab. Perhaps in a few years, when I grow old and no longer use Matlab, this will serve as a conclusion to my 10-year journey with Matlab.

1

Introduction

To be precise, this article is not intended for complete programming beginners, especially those with no prior programming experience.

The target audience for this article is individuals who already have a certain programming foundation. If you want to use Matlab to quickly complete algorithm verification and academic research, this article is for you.

The scope of this article is vast, and the amount of information is significant. Beginners may not understand some issues, so please search on Baidu. As for those with programming experience, most of the content mentioned in this article should be understandable. With the right mindset, you should be able to navigate Matlab software easily.

The version of Matlab chosen for this article is: 2012a. Personally, I think this version has always been quite good. Although newer versions have a localized menu, the UI is really ugly, and I have no words to criticize it. There’s no need to upgrade.

Additionally, I want to remind all Matlab users that for 80% of people, lower versions are really sufficient. There’s no need to pursue higher versions, especially for those who don’t even know what cracking or toolbox selection means. Choosing a lower version may lead to fewer troubles. Many people encounter issues because they haven’t installed the toolbox, making it impossible to find functions…

2

Brief Introduction to Matlab

Currently, the main users of Matlab in China are students and research institutions. I’ve seen some usage in commercial environments, but it’s rare. I won’t speculate or comment further.

Why is Matlab so popular? I summarize it in four aspects:

1. Efficient Numerical Computing Functionality. This is outstanding; currently, no other programming language or similar mathematical software can replace it;

2. Comprehensive Computational Results and Programming Visualization Functionality. This is also outstanding and irreplaceable; my blog has a Matlab column, most of which deal with C# calling Matlab for mixed programming. Every year, many students and other engineers have this need: directly calling Matlab’s Figure… and embedding it into WinForm… This is closely related to Matlab’s excellent visualization capabilities;

3. Friendly and Complete Programming Development Environment, along with the natural m language that closely resembles mathematical expressions. It’s very easy to learn and master; Matlab software is a programming development environment, and the built-in m language is simple and easy to use, making it very accessible for those with programming experience;

4. Rich Functional Application Toolboxes and Help System. Currently, the total number of Matlab toolboxes has exceeded 80, covering various fields such as mathematics, statistics, simulation, electronics, bioinformatics, finance, testing, and more. Rather than saying Matlab is powerful, it’s more accurate to say its toolboxes are powerful. Each toolbox includes classic algorithms and processing methods in the industry, saving a lot of trouble, allowing for quick verification of ideas and implementation of algorithms in research.

3

Introduction to Matlab Development Environment

To use Matlab, installing Matlab is essential. Below is the main interface of Matlab, and I will introduce the functions of several main windows. This article uses Matlab 2012a; other versions with Ribbon UI have similar windows, aside from menu differences.

Summary After 10 Years of Using Matlab: Key Points to Master

The above is the main interface of Matlab; I’m using version 2012a. The newer version after 2013 may have some changes, but it feels uglier… I’m still used to this one. However, the functionalities are mostly similar. Let’s look at the functions of each part; I won’t discuss the menu bar for now, as the names are generally understandable. Let’s explain the roles of each part in the image above.

1. Current Folder: Current Path. This is the path of the current Matlab working folder, which generally does not change after being set upon startup. Its utility is limited, but sometimes it helps locate files; it loads your frequently used Matlab directories, and you can quickly switch to different Matlab working paths using the drop-down arrow;

2. Current Folder: This also displays the files in the current working folder. It shows all resources in the folder, which is easy to understand. When you need to open a file, just double-click the corresponding m file;

3. Individual m File or Function, when clicked, will show the contained functions in 4, similar to how VS displays properties and methods in a class, allowing you to understand the file structure;

4. Functions Included in the Selected File in 3, allowing you to see the function structure in the m file without opening the folder;

5. Command Window is the main window, where commands are entered. It’s the most important area; you can perform simple tests and learn commands here, but once you’re accustomed to it, you may prefer using m files;

6. Workspace is the space for working variables, mainly displaying the values of variables existing in the current Matlab session, including variable names and values. If it’s an array, it shows the maximum and minimum values. This area is mainly used for debugging programs, similar to local variable values after setting breakpoints in VS, but here it displays more intuitively and is very useful.

7. Command History is the history command window. A little trick here is that commands you entered in the command window will be displayed here, and you can use the up and down keys on the numpad to quickly jump to previous commands, making testing more convenient;

4

Common Commands

For those who frequently use Matlab or conduct simple tests, it’s essential to master and know some common commands. If you can’t remember the following commands, you can search for the corresponding functions based on their functionality on Baidu; it can be done quickly. Once you’re proficient, it only takes about 30 seconds to recall them, and eventually, you’ll remember them.

1. clear : Clears memory variables and functions, effectively emptying the Workspace variables;

2. clc : Clears the contents of the current Matlab command window; it’s like clearing the screen to start anew. Note that clc does not clear variables. Generally, when writing m files, if it’s not a function, you typically start with clear; clc; to ensure that memory and the screen are cleared during m file execution to avoid interference from variables with the same name and to make it easier to observe the screen;

3. help : This is one of the most important functions. When you want to know the related information of a certain function, you can use help function_name to get it, or you can open the help documentation. However, this method is quicker.

4. zeros: Creates a matrix filled with zeros; ones: creates a matrix filled with ones. The parameters can be multi-dimensional, for example, zeros(2,3)…

5. size: Can calculate the size of a matrix; similarly, related functions include length. Size can calculate sizes in different dimensions, for example, size(A,1)…

6. rand : Random number generator, can directly generate matrices of any dimension, for example, rand(2,3), which is also a commonly used function;

7. plot: The operation for plotting images; you can refer to the help for specific formats. This operation is very frequent; when doing research, you often need to observe trends, and plotting in Matlab is a common task… There’s also a subplot function that divides a figure into multiple segments for operations. Think about how I used to plot with mschart in C#; it was quite a hassle… However, I still have to persist. Over the years, why have I abandoned Matlab? Because everything Matlab can do, I’ve been doing with C#, which is a long story to discuss later.

The most important functions are introduced here. To master the core syntax of Matlab, you should also look at the basic syntax for matrix operations in Matlab, as it’s very flexible. Mastering a few basics can accomplish a lot.

5

Matrix Operations That Must Be Mentioned

As mentioned earlier, one of the most important aspects of Matlab is its powerful numerical computing functionality, which is not only reflected in its robust function library but also in the flexibility of its operation syntax. Just pick up any introductory book on Matlab programming, and it will certainly cover this aspect. In fact, the flexibility of matrix operations is so powerful that I dare not look at it. In daily tasks, mastering a few basic operations is sufficient. Here, I would like to highlight a few that can significantly improve your efficiency.

5.1 Initializing Sequences

When writing various algorithms, you often need to initialize matrices. For example, the previously mentioned zeros, ones, etc. are all methods to generate matrices. There are also other methods to create diagonal matrices, etc. Here, I want to particularly mention methods for generating data or arithmetic sequences. For example:

a = 1 : 10 ;% Generate an arithmetic sequence from 1 to 10 with a default step of 1;
b = 0:0.1:1;% Generate an arithmetic sequence from 0 to 1 with a step of 0.1;

Isn’t it concise and efficient? In other programming languages, you might have to encapsulate a method for this, but in Matlab, everything is simplified.

5.2 Matrix Operations

In Matlab, arrays, or matrices can be expanded freely without additional requirements, and changing matrices is effortless without needing for loops. Let’s look at a few examples; this will give you a glimpse:

Summary After 10 Years of Using Matlab: Key Points to Master

data = rand(4,4) % Initialize a 4x4 random matrix
a = data(:,1) % Get all rows of the 1st column
b = data(:,[1,3]) % Get all rows of the 1st and 3rd columns
c = data([2,3],[1,2]) %% Get the 2nd and 3rd rows, and the 1st and 2nd columns

Summary After 10 Years of Using Matlab: Key Points to Master

Looking at the results below, it’s quite intuitive:

Summary After 10 Years of Using Matlab: Key Points to Master

There are more examples, such as find, but I won’t elaborate on them here… This is just the tip of the iceberg…

6

Programming Syntax

After reviewing the syntax above, are you intrigued? Next, let’s look at some basic syntax of Matlab’s m language. We can perform some simple tests in the command window, but it’s not suitable for opening up. When we need to complete a complete logic, we will use the m file editor. Open the editor, and the interface looks like this; you can start writing… This development environment is also very robust, allowing for breakpoint debugging and step-by-step execution. This is a summary article, and I don’t have any Matlab books around me, so I’ll write whatever comes to mind.

Summary After 10 Years of Using Matlab: Key Points to Master

1. Variable names in Matlab are case-sensitive, and there are no specific naming rules in official functions; most start with lowercase letters, which you will get used to.

2. Note that if a statement ends without a semicolon, the variable’s value will be displayed directly in the Command Window; adding a semicolon will only display it in the Workspace. This is also the case in the command line window; you can test the difference between a = 100; and b = 100.

3. In Matlab, the comment symbol is %.

4. Variables do not need to be defined; you can give it an empty value [] and assign other values later. Its length is not fixed; you can increase it as you wish.

5. Among Matlab’s data types, I find the character and cell arrays quite peculiar. Cell arrays are a mixed bag that can accommodate various complex types, while character types have caused me many headaches, and I often get stuck. Here’s a tip: concatenating strings, paths, or arrays in Matlab is not done like in other programming languages. You can use + to do this, like:

[‘ab’ ‘cde’ num2str(2)] % Note the space in between, resulting in: abcde2

6. Although matrix operations are very flexible and most can be completed in a single line, loops are still sometimes necessary. Matlab also has for and while syntax. Note that every layer of for or while must correspond to an end.

7. Matlab now also supports object-oriented programming, but I don’t use it much. I mainly use Matlab for functional programming… If you’re interested in object-oriented programming, you can look for relevant materials. The most commonly used functions in Matlab are similar to methods in other languages. However, Matlab functions do not differentiate between static and instance methods; overall, they are all static and called directly.

The keyword for defining functions is function, and input and output parameters can be multiple, for example, function [r1,r2,r3] = testfun(p1,p2,p3).

One note: functions cannot be run directly. You can write an m file to call the function you wrote. Those with programming experience should understand this. I want to emphasize that when calling Matlab functions from C#, it must be a function to be compiled and called; non-functions are not supported.

7

My Thoughts on Matlab

Although I can do many things with Matlab, I have completely moved away from it now. At first, I wasn’t used to it, as I had to write many mathematical functions myself and explore them. However, through gradual accumulation and discovering open-source resources, I have started using the Math.NET component to replace its numerical computing part. While the syntax isn’t as elegant, it functions well. As for other functional components, such as statistics and data mining toolboxes, there are corresponding open-source components to accomplish them. If I can’t find what I need, I can implement it based on algorithms, which isn’t difficult. Thus, during this process, my skills in C# have also significantly improved.

This doesn’t mean Matlab is bad. The biggest issues with Matlab currently, from a practical application perspective, are several:

1. The enormous MCR runtime, which once made the .NET environment of several tens of MB unbearable, let alone Matlab’s MCR, which is over 300 MB. After installation, it exceeds 1 GB…

2. The interaction with other platforms is still not very flexible or convenient. For instance, calling Matlab’s m functions from C# is quite good now, but there are still efficiency and usability gaps that hinder its widespread adoption;

3. Although Matlab has invested a lot of effort in distributed computing, efficiency, and deployment, its price is daunting… I haven’t found cracked versions of several toolboxes… It’s a hard truth; even if you buy it, hiring people with such skills is a challenge…

4. The resources available for Matlab are too few. Currently, mainstream open-source project hosting platforms have very few Matlab projects. Although the official has a forum for open-source exchanges, it’s not very popular and is also in English, which is not very useful. Moreover, domestic researchers work independently, and they hardly share any Matlab code they’ve written, resulting in repeated code writing by future generations…

Every time I install a system, I always inexplicably need to install Matlab, but it seems I rarely open it. I hope this isn’t the end. Matlab is indeed a good tool.

Lastly, let me mention Matlab’s help system: several years ago, when Microsoft’s MSDN had an offline version, it was probably the best help system. However, now MSDN seems to no longer be included in the VS installation package, and the offline version is not very meaningful. But Matlab has continued to do this, which is one of the reasons for its strength. Everything can be found in the documentation. However, you need to have a basic understanding of English, which is also a significant drawback. There is no Chinese version, but thinking about it is terrifying; translating so many articles into different versions must be a huge workload. In the past, there were forums translating functions from these documents, but it seems they were halted by the official for copyright reasons. It’s a pity; they provide free publicity for you, and yet this happens… Finally, here’s a picture of the help system I once struggled with:

Summary After 10 Years of Using Matlab: Key Points to Master

(Source: Data Summit Blog Original Link: http://www.cnblogs.com/asxinyu/p/Basic_Matlab_Experience.html)

Leave a Comment