Welfare at the end of the article: MATLAB R2022b software installation package
MATLAB
Introduction
01
1. Introduction to MATLAB
Mathematical science and technology application software includes numerical calculation (Number Crunching) type software and mathematical analysis (Math Analysis) type software
numerical calculation software
They have strong management, calculation and visualization capabilities for large amounts of data, and high operating efficiency. Representative software includes Matlab, Xmath, Gauss, Mlab, etc.
Mathematical analysis software
They are good at symbolic calculations, and can obtain analytical symbolic solutions and arbitrary precision solutions, but they are inefficient when dealing with large amounts of data. Representative software includes Mathematica, Maple, etc.
In summary, Matlab has five major functions:
1. Numerical calculation function (Numeric);
2 symbol calculation function (Symbolic);
3. Data visualization function (Graphic);
4. Unified processing function of data, graphics and text (Notebook);
5. Modeling and simulation visualization function (Simulink)
For non-computer majors, in many cases, when they need to complete a task, they often choose Matlab first instead of programming in a certain programming language. I think an important reason is that Matlab's user interface is very friendly, programming is more efficient, and it is easy to learn and use.
The following is a brief introduction to the interface of Matlab.
If you need to complete more complex tasks, you can also right-click in the working folder, create new, script, and create a .m file, which is equivalent to the .c and .cpp files in C language. Then double-click the .m file and an editor window will pop up above the command line. Pull the border to adjust the size of the command line and the editor. If you press and hold the top of the window and drag it out, you can also realize a separate window for the editor.
Next, make a brief introduction to the toolbar and menu bar.
1. The file column is mainly for the operation of script files
2. The variable bar is mainly for the operation of the variables in the workspace, but generally it is rarely used, because if you want to view the variables, you can directly double-click in the workspace to view them
3. In the environment part, pay attention to the two buttons of preset and setting path. Preset is equivalent to setting, which can adjust the interface, font, display language, etc.; setting path is needed when adding a third-party toolkit, which can be understood as adding a plug-in
This toolbar is for the editor, that is, it will pop up automatically when the editor is opened. This interface is mainly for various operations on the code
Note: The shortcut key is CTRL+R
Uncomment: The shortcut key is CTRL+T
Run section: Compared with other IDEs that need to select a certain section of code to run part of the code, Matlab provides an efficient way, that is, set the section break %% (remember to add a space after it), then select a section with the cursor to run a certain section of code.
Setting breakpoints: Matlab setting is as convenient as VSC, just click the red dot on the left side of the code
Clear breakpoints: Click the triangle under the breakpoints, and you can choose to clear all breakpoints.
02
2. Getting Started
Basic introduction to the command window
The command window must be the first thing a novice comes into contact with when getting started. Its biggest feature is that what you see is what you get, that is, you can input a command at will in the command line and press the Enter key to get the calculation result. The speed is very fast, as shown below:
Enter "1+1" and press Enter to get the calculation result 2, where ans is the abbreviation of answer, which can be regarded as a variable that comes with the system and will be updated continuously.
It should be noted that with the input of the command line, the variables that appear during the operation will be automatically added to the right workspace (refer to the interface diagram in the introduction), for example, an ans variable appears on the right at this time, and the displayed value is 2.
Note: These variables will always exist if they are not cleared, and their values will remain unchanged unless the variables are reassigned on the command line. Also, if the variable is a matrix, instead of showing the exact value, it will show the size of the matrix.
Press Enter on the command line to display the result, so can it not be displayed? Of course you can, just add an English semicolon at the end.
Here are some more frequently used basic instructions:
demo : Enter demo and press Enter to pop up the locally installed help document. Of course, you can also use a browser to access the online help document—Matlab online help document, make good use of the search function!
help : A useful tool for finding specific functions or algorithms! It is equivalent to the command doc, and the method of use is help plus the content to be searched.
Similar commands: helpwin (simplified version of help) and helpdesk (used alone, located on the home page of the help document)
clc: Clear the contents of the command window (similar to the clear screen function of the serial port terminal)
clear : Clear the variables in the right workspace (use with caution! Be careful to distinguish it from clc!)
format : Set the format of the data, as follows:
format short (this is the default) 4 decimal places
format long 15-digit scientific notation
format short e 5-digit scientific notation
format long e 15-digit representation, etc.
In addition, format rat indicates that the result of the operation is expressed in fractions.
ver : Use alone, see MatLab and Windows versions.
who : Display the names of all current variables
whos : Display detailed information of all current variables.
pack : tidy up the memory of the workspace
load : Import the workspace from the file (usually a file with a mat suffix)
save : Save all variables in the workspace into a file, generally saved as a file with a mat suffix
what : Display the specified Matlab file.
lookfor : Search for keywords in help (the sorting principle is to sort the search content in alphabetical order)
which : locate function or file
path : get or set the search path
echo : command echo
cd : change the current working directory
pwd : Display the current working directory (this is very universal)
dir : display directory contents
unix: Execute unix commands
dos: execute dos command
! : Execute operating system commands
computer : Displays the computer type.
03
3. Common operations
a. Manually change variables
If you don’t want to modify by command, you can also double-click the variable, and a variable interface similar to Excel will pop up automatically, you can view or modify the variable
b. Reuse of front and back instructions
When you want to repeatedly calculate a very complicated expression on the command line, what if you don't want to retype it every time? You can choose to copy and paste, but MatLab provides a more useful method, which is to use the up and down keys to select the previously entered command, which is also a very commonly used function.
c. Terminate the current operation
When you accidentally write an infinite loop or run a huge data file and find that the status bar in the lower left corner shows busy all the time. If you want to terminate the operation but don’t want to close the software, you can use the process termination operation, Ctrl + C; in addition, if you want to close Matlab. You can enter Exit or Quit on the command line, which has the same effect as clicking the cross in the upper right corner.
04
Fourth, the editor window (Editor Window)
The editor window is used to create .m script files, and the difference from the command line window is like the text editing and terminal input of VS Code. The method to call out is to click New in the menu bar and an editor window will pop up automatically:
If you choose to save, it will be saved in the current folder on the left by default, similar to a workspace.
However, it should be noted that the naming of MATLAB script files is also particular:
1. The file name should be named in English characters, and the first character cannot be a number or an underscore;
2. The file name cannot be the same as the internal function name of matlab. The name of the m file name should not be a simple English word, it is best to be composed of uppercase and lowercase English/numbers/underscores, etc. The reason is that simple word naming is easy to have the same name as the internal function name of matlab, and some inexplicable errors will appear as a result;
3. The file storage path should preferably be in English. If you use a path with Chinese, there will be problems in some cases.
4. There cannot be spaces in the file name of m. If you need to use a combination of two or more words as a file name, each word must be directly connected to the combination (you can capitalize the first letter of each word for distinction, or use an underscore). Such as random walk, it should be written as RandomWalk or Random_Walk.
Click to receive MATLAB R2022b software: MATLAB R2022b software installation package