Join Patrick Royal for an in-depth discussion in this video Viewing and editing programs, part of Learning MATLAB (2013).
Lets look at the bread and butter of MATLAB. Viewing, Editing, and Running programs. Creating a new program is easy. Simply click on the New Script button, to generate the script and automatically open up the Script Editor window. Here, you can type in all the code that will run as a part of the program. For this example, we'll just create a very simple script that generates a random two by three matrix. So P equals rand 2,3. When you're ready to save, click on the Save button and then give the program a name.
And it will automatically save to the active folder. Now, any time you want to get back to the script to edit it, simply double-click on the name in the current folder pane and this will reopen the window and allow editing of the script exactly as before. To run the program, there are several different option. First you can click on the Run button within the Editing window. This runs the program immediatly without closing the window, allowing you to quickly and easily see how changes to the program effect the output. Second, you can right-click on the script from within the current folder window. And choose Run or press F9.
Finally, you can run the script by typing it's name into the Command Window and pressing Enter. This final method is esspecialy useful if your script is a function, since this is the only way to provide the inputs. When running a function, it is important to make sure you provide the correct inputs in the command statement. Each argument must be a variable of the proper type and dimensions in order to be acceptable. Unlike other programming languages, MATLAB doesn't have a variable definition statement.
So it will not be able to tell you what format or what dimensions you need for the variables, just their names. Instead it will attempt to run the function with whatever data is provided which can lead to unpredictable behavior. Because of this, it's good practice to make the variable names in your functions as descriptive as possible. For instance, rather than naming the variable p, it would be a good idea to change the theme to something like stock prices. It's also never a bad idea to add checks in the program.
For instance, if your function requires a 3 by 3 matrix, the first line of it might be to check if the matrix dimensions are 3 by 3. If not, the function would exit and throw an error message. That's the basics of editing and running programs within MATLAB.
- 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