MATLAB Concise Tutorial #1

Original link: https://www.bytecho.net/archives/2021.html

Getting Started with MATLAB

If you can use MATLAB proficiently, you will undoubtedly open the door to explore the origin of everything in the universe. —Henry E68D82E598B4E7AC91_2x.png

Enter MATLAB

image.png

Well, when we open MATLAB, the most striking part is the command line window , where we try to enter some simple commands and start learning MATLAB.

basic commands

simple calculation

Unlike other high-level languages ​​(C++, Java, Python, etc.), MATLAB does not require strict variable definitions. Try to enter 6*8 in the command line window and run, you will find that MATLAB outputs a variable value named ans, This is the result of the 6*8 operation.

define variable

To define variables in MATLAB is just as easy, try the following statement:

 m = 3 * 5

This successfully defines a variable named m.
Let’s take a look at the assignment operator, which, like the high-level languages ​​we’ve learned, is a simple equals sign:

 m = m + 1

Not to be surprised, the effect is the same as in C++, the value of m is changed to m 本身+ 1 .

1GIEPV2L4PTJOJ28.png

At this point, we open the workspace on the right side of the MATLAB program, and you will find that both the ans variable and the m variable we defined appear in it. The workspace displays the variable’s name, value, size, and type, which is very eye-catching.

Grammar comparison

It should be noted that perhaps we have found that the previous command seems to be different from C++, it does not end with a semicolon, in fact, the semicolon in MATLAB is not the same as C++ and so on.

Adding a semicolon to the end of a command will suppress the output, but the command will still be executed, as you can see in the workspace. When you enter a command without ending with a semicolon, MATLAB displays the result at the command prompt (executes the command directly).

Introduce a little trick, friends who have used the Linux terminal should be familiar: you can press the up arrow key on the keyboard to recall the previous command.
Note that to do this, the command line window must be the active window.

Save and load variables

You can use the save command to save variables in the workspace to a MATLAB-specific format file called a MAT-file.

To save the workspace to a MAT-file named foo.mat , use the command:

 >> save foo

Use the load command to load variables from a MAT-file.

 >> load foo

After loading, the variable data will be listed in the workspace. You can view the contents of any variable by entering the variable’s name.

 myvar //你的变量名

Tips:

Use clear to clear the workspace.
The clear function clears the workspace and the clc command clears the command line window .

OK, that’s all for today.


Byte Planet Henry 2022-07-07 Reprinting is strictly prohibited without permission!
https://www.bytecho.net/archives/2021.html

This article is reprinted from: https://www.bytecho.net/archives/2021.html
This site is for inclusion only, and the copyright belongs to the original author.

Leave a Comment