3. Creating Scripts in MatLab - Arduinerd/PDS-MatLab GitHub Wiki
It is possible to run multiline commands within the command window, however, it is more difficult to edit and less readable.
Creating a Script
Script Templates
- Choosing function from the NEW button will create the template for a function in a new Editor window
- Opening an Editor window will open the EDITOR, PUBLISH, and VIEW tabs in the ribbon
Pressing ALT+ENTER next to a squiggly line will apply the recommended fix
- Choosing the example button from the NEW button will give a new file showing a title and two sections
- Choosing class from the new button will open a new class definition
Discoverability and Path
- In order to invoke a script you must be in the same file location
- If you are not in the same folder, you must add it to the MatLab path
- Trying to run the function from a different location (while it is open) will prompt you to set the path
File structuring and Sections
- %% denotes a section
- %% must be used to end a section
- You can also add in a section from the Add Section or Add Section Title from the Publish tab
- Within the editor tab you can run a whole script or by section according to the button in the EDITOR tab
Comparison and Search
- Under the find files button, you can search for a file name, file text, or others
- You can restrict your search in several ways
- The comparison button allows for text comparison to see where text is added or deleted
- You can save the result as an html file
Debugging
- Breakpoints pause the program at a particular point
- Breakpoints can be set by clicking the line number, selecting the menu item, or pressing F12
- Within the EDITOR tab you can control execution after hitting a breakpoint
Publishing Scripts
- By creating comments within sections, you can creates paragraph text that will appear upon pressing the publish button in the PUBLISH tab
- You can format the text in several ways to appear as a html file
- After adding code, both the code and the result will be visible after publishing
- You can add LaTex formulas in the middle of a paragraph
Using LaTex
- Within the Symbolic Toolbox is the MuPAD notebook which helps write LaTex
- The app allows your to copy the input as LaTex code
syms xcalls the Symbolic toolboxlatex(“function”)will print out the LaTex code for a function from the command line- LYX is a program which will achieve the same effect
LaTex is documentation preparation system which allows users to input mathematical formulas as plain text. It is preferred by most of the academic world for papers and other official documents.
Control Statements
Control statements allow for the user to direct the flow of a program given certain conditions. Thus given certain data, the program can skip, repeat, or stop at many different points.
Conditionals
if's,elseifs's, and else's allow the user to test if a condition is true. It is possible to involve complicated logic given the conditional operators listed on page 3 in addition to others.
if condition1
code to be executed if condition is true
elseif condition2
code to be executed if condition2 is true and condition 1 is false
else
code to be executed if condition1 and condition2 are false
end
Switch's allow for a much more compact way of testing a single variable for multible values. The default keyword is the code to be executed if none of the cases match.
switch variable
case value
case value2
default
end
Loops
For loops execute a block of code while an iterator value (usually i) follows a given constrant
for i=1:5
x(i) = i^2
end
% Result: x = [1 4 9 16 25]
While loops execute a block of code while a condition is true. Unlike a for loop, they do not use an iterator that automatically increments. As such it is possible to run an infinite loop (Stop this with CTRL+C)
while condition
code to be executed
end
If you want to exit a while loop before the condition is false, you can use the keyword break to terminate the loop
Functions
- functions require input and output arguments
function [out1, out2] = functionName (in1, in2,…) - The editor will warn you if an output variable is not set or an input variable is not used
- By typing functionName(in1, in2) into the command window you will run the function (if file is within path)
It is highly recommended that you include comments in your functions and scripts so that you and others will be able to understand the code quickly
Objects and Classes
MatLab supports object-oriented programming, however it is not necessary for basic use of the language
Classes
- Basic superclasses are either handle and value
- Value class (similar to variables)
- Handle class (can be shared and referenced)
- A class is declared by
classdef “name” < “type"with a correspondingend
In order to create an object:
“variable” = “Classname”
- You can make arrays of classes
Properties
- Must define a properties block (with end)
- Variable names and values are defined here
Methods
- Must define a methods block (with end)
- Methods are written as functions, however one of the arguments is always the Object or obj
- In order to refer to a variable within a method you must write obj.”variable“
- Once variables are created, they must be recreated in order to update all changes
Events
- Declaring methods (static) will create methods that do not require an object to function
- Static methods do not require a reference to the object
- You must define an events block (with end)
- Addlistener (object, event, @(src, e) “Static Method”)
Inheritance
- < operator is the inheritance operator
- You can inherit from more than one class
Enumerations
- Must declare an enumeration block (with end)
- Declare values (Example: Open, closed)
IMPORTANT, you must put an end statement at the end of every control statement, function, or class. Otherwise this will cause various errors.