Learn MATLAB: Essential Tips and Tricks

Learn MATLAB: Essential Tips and Tricks

This article mainly introduces the most basic and core concepts of MATLAB and the most useful software interface operations. If you are just starting to learn, you can fully understand this article. If you have any suggestions, you can leave a message to the editor in the background.

The screenshots in this article are from version 2019b, but the interface differences between various versions are not significant. When installing, try to choose the b version, which represents the second half of the year it was released, as it is usually better than the a version released in the same period.

Learn MATLAB: Essential Tips and Tricks

Learn MATLAB: Essential Tips and Tricks

(Dynamic plotting, source code at the end)

1. Introduction: First of all, MATLAB is short for “Matrix Laboratory”, meaning its full name is Matrix Laboratory, not Mathematics Laboratory, which can easily confuse beginners. This means that MATLAB emphasizes matrix (or array, as we often mix these two concepts) operations, while most other programming languages use loops to process values one by one (i.e., matrix elements), like C++. Therefore, all MATLAB variables are actually multi-dimensional arrays (which can be understood as a generalized matrix). Thus, in programming, one should be cautious: in MATLAB, try to minimize the use of loop statements, especially nested loops, and instead adopt matrix operations. Larger code bodies will show the disadvantages of loop statements in MATLAB. Statistically, under the same loop, MATLAB’s execution time for loops will be about 20 times that of C++.

Learn MATLAB: Essential Tips and Tricks

For specific basic operations on arrays, refer to the screenshots below:

Learn MATLAB: Essential Tips and Tricks

Learn MATLAB: Essential Tips and Tricks

2. Interface: The MATLAB startup page looks like the one below; the differences between multiple versions are not very significant, and the default three-column layout is unique to MATLAB. The left side is the “Current Folder“, which tells you which folder the code is currently running in; if no path is specified, the generated files will also be saved in this folder by default; at the same time, files in the current folder can be imported without specifying a path. The largest section in the middle is the “Command Window“, which can be understood as a window where you input one line of code and execute it; all error messages will also appear here. The rightmost side is the “Workspace“, used to store the values of variables, where you can see which variables are in the current results.
Learn MATLAB: Essential Tips and Tricks

Saving variables in the workspace is very important, and it is easy for beginners to overlook. The .mat file is a file for storing variables; also, make good use of the save and load functions.

Learn MATLAB: Essential Tips and Tricks

If you are not satisfied with the current page layout, you can set your own style. In [Home] – [Layout], you can change it and save the settings, one-click restore. Set according to personal habits for easy restoration after dragging; a comfortable interface can give you a good coding mood. Overall, the default layout is suitable for entering one line of code at a time, while it is not very suitable for editing large .m files; at this time, you can choose the layout in the second image below (customized by the editor); even if the layout is messed up as in the third image, you can one-click restore the layout to the second one.
Learn MATLAB: Essential Tips and Tricks
Learn MATLAB: Essential Tips and Tricks
Learn MATLAB: Essential Tips and Tricks
3. Statements: When using MATLAB, you can issue commands to create variables and call functions. For example, by typing the following statement in the command line, you can create a variable named a without having to define the variable type in advance:
Learn MATLAB: Essential Tips and Tricks
If the statement ends with a semicolon, MATLAB will perform the calculation but will not display the output in the command window. That is, the semicolon (must be the English semicolon) is used in MATLAB to suppress output to the command window. Since output also takes time, we will add semicolons to non-essential statements later. Many errors are caused by using a Chinese semicolon, so please be careful to check.
4. Command History: Place the cursor at the command line window >> position, then press the up arrow key to view previously entered commands to save time; even after closing MATLAB and reopening it, those historical inputs will still be there, just so you know.
Learn MATLAB: Essential Tips and Tricks
Learn MATLAB: Essential Tips and Tricks
5. Function Browser: When we press shift+F1, the built-in Function Browser will pop up, which aggregates all functions, clustered by functional modules. You can take a look when you have time to understand MATLAB’s capabilities. Before you try to write a function from scratch, it’s wise to check if MATLAB already has it built-in. Additionally, when you want to execute a certain function but don’t know what function to use, or have forgotten the specific name or the parameters to input, you can bring up this window to search above.
Learn MATLAB: Essential Tips and Tricks
Generally, use keyword search; common functions already have Chinese descriptions that are easy to understand. When hovering over a function, a more specific description will pop up. We need to pay attention to the data types and counts of function parameters and return values, or else errors will occur.
Learn MATLAB: Essential Tips and Tricks
Learn MATLAB: Essential Tips and Tricks
6. Function Hint: When we write a function but forget what parameters should be input, we can place the cursor inside the parentheses after the function name and use the shortcut ctrl+F1 to quickly view hints for the function.
Learn MATLAB: Essential Tips and Tricks
7. Function Introduction: In the command line window, type help function_name to pop up a detailed introduction to the function;
Learn MATLAB: Essential Tips and Tricks
In the command line window, type doc function_name to pop up the most detailed function introduction in the Function Browser; it will provide example code that can be copied into MATLAB for operation, as well as very detailed principles and operation instructions. If your English level is good, you can visit the English page for more comprehensive information.
Learn MATLAB: Essential Tips and Tricks
8. Help Document: Make good use of the help document, which is located in the top right corner and is very important. It can serve as a reference book for consultation; you can take a look when you have time, as it includes not only code introductions but also introductions to algorithm principles. You can search or browse through the directory tree.
Learn MATLAB: Essential Tips and Tricks
Learn MATLAB: Essential Tips and Tricks
9. Comments: In MATLAB, % indicates a comment, and % will continue until the end of the line, but it will not apply after a line break.
Meanwhile, two consecutive percent signs %% represent a section; appropriately using sections can make the code look neater. For example, in the second image below, the selected section will turn green, while others will have no major differences.
Learn MATLAB: Essential Tips and Tricks
Learn MATLAB: Essential Tips and Tricks
10. Line Break: You may have noticed a thin line on the right side of the MATLAB window (as shown above); this is not a malfunction of your monitor, but rather the MATLAB print output control line; if a line of code is too long, it will cause problems when printed on A4 paper, so this is a reminder line, although you can actually ignore it. However, to keep things neat, it is best not to exceed this line with a single line of code; you can use line breaks. To break a line, input three English dots at the point where you want to break. The original two lines should be in the first image, and the effect of the line break is shown in the second image, where successful line breaks should have automatic indentation. Additionally, if you feel that the code indentation is messy during programming, you can select all code and press the shortcut ctrl+I to auto-indent.
Learn MATLAB: Essential Tips and Tricks
Learn MATLAB: Essential Tips and Tricks
To learn more MATLAB skills and useful software,
you can click Read Original,
or scan the QR code below to follow the Tu Channel WeChat public account~

Learn MATLAB: Essential Tips and Tricks

Leave a Comment