Join Patrick Royal for an in-depth discussion in this video Basic commands, part of Learning MATLAB.
Let's go over some of the basic commands and syntax that you'll need to write scripts in MATLAB. MATLAB is a high level programming language, so most of the syntax is relatively simple. Let's start by opening up a new script by clicking the New Script button. Creating and defining scalar variables in MATLAB is very easy. All you have to do is type variable name equals the value and the variable will be created. The variable can then be referenced by typing in that same variable name. If you want to define a variable that contains multiple scalars, a vector or a matrix, put brackets around the definition, and then separate indivudualy values with commas and spaces, and rows with new lines or semicolons.
For instance, a equals 4, 0, 2; 1, 2, 3, defines a as a two by three matrix with 4, 0, and 2 on the top row, and 1, 2, and 3 on the bottom row. You can see this if we run the script, and then switch back into the command window. Now, if we go back into the Script Editing window, and we change this to a equals 4 0 2, new line, 1 2 3, and then run the script again.
Our Command Window will display the same result. To define variables as text rather than a numerical value, put single quotes around the text you're defining. If you want your string to be variable rather than fixed, the functions num2str and strcat will be useful. Nam2str converts a numerical value to a string, allowing for a non-pre-determined number to be added to the string. Strcat concatenates two strings horizontally to form one longer string. For instance if we switch back to our script, and defined b equals single quote hello and C equals single quote world.
We can then say that d equals strcat of b comma c, and then when we run this and switch back to the command window, we'll see that b equals hello, c equals world and d combines the two of them. There are two other non intuitive characters that will be useful in writing scripts in MATLAB. To transpose a matrix, put an apostrophe next to it. This is an inline function, so you can transpose multiple matrices within a single function without needing to define new matrices on separate lines.
So if we go back to our script, and we type in a transpose, and then run it. The command line will now display a with columns instead of rows. The second special character is a semicolon. Unlike Java, MATLAB does not require you to place a semicolon at the end of every line. If you do though, then MATLAB will interpret it as a command to suppress output for whatever is happening on the previous line. This is an extremely useful function, because by default, MATLAB displays results of every single function, equation, variable definition or loop as the program runs. For larger scripts, this can quickly overwhelm the command window and make the program unusable.
In general, unless you specifically want to see the output from a line, it's good practice to end every line with a semicolon. So, if you go back to the script editor, and we put semicolons at the end of every line, and then run the function again. We can see in our command window that this function has run as normal, all of the variables are exactly the same as they were before, but it hasn't added all of that extra output to the command window.
- Installing MATLAB
- Working with MATLAB variables
- Working with matrix and scalar operations
- Creating functions
- Understanding performance considerations
- Building basic plots
- Creating responsive programs
- Editing variables manually
- Working with the Statistics Toolbox