2. Basic Programming Principles - Arduinerd/PDS-MatLab GitHub Wiki

Data Types

An important thing to note is that you do not need to implicitly declare a type like other languages. MatLab will automatically assign a type based upon the value assigned.

>> x=2;

The whos "variable" command will output the variable name, size, memory, class, and attributes. It is important to note that data types are not static and will change if the value assigned is of a different type.

Remember to put a semicolon on the end of a statement unless you want to print out all returned values.

Numbers

  • Any numeric value is automatically stored as a double (floating point decimal)
  • Numbers can also be stored as a short which 4 bytes of data while a double is 8 bytes
  • There are different maximums and minimums for different types, so it is recommneded to stick with doubles unless there is a pressing need

Constants

  • Inf (Infinity) - an example of this would be trying to divide by zero
  • NaN (Not a Number) - an example of this would be 0/0 or inf/inf
  • i and j represent the complex constants

Strings

  • An array of characters, represented by " "
  • A single character is denoted with ' '
  • Concatenation is done through [str1 str2]
  • num2str(“number”) will convert a number to a string
  • disp(“output”) the function to output a string to the console
  • strcmpi() compares two string

While MatLab has many operations for strings, it designed primarily for computation with numbers

Structure

  • A variable with a user-defined field

>>Student.age = 0;

  • New fields can be defined at any time

Cell Arrays

  • An array containing multiple data types

>>Student = {18, "Christopher"}

  • Typing variable(index) will return the element with the cell wrapper class
  • Typing variable{index} will return the element with its original data type

Arrays/Matrices

When running the whos command, you might see that every variable has a size and that even a number will be liste as being 1x1. This is because every variable regardless of type is an array.

In MatLab Arrays and Matrices are the same things and are used interchangebly

  • Similar to a cell array, variable(index) will return the value
    • Indexes start at 1 and NOT 0
    • When indexing within matrices, indexing is either by x(row,column) or by x(index) which is its position within the linear description of the matrix in row-major order (starting at top left and going down)
    • Ranges and stepping also works with indexing by x(start:end) or x(start,step,end)
    • The ":" operator select the entire range

In order to instantiate an array:
>>x = [1 2 3 4];
Adding ";" will add another row:
>>x = [1; 2; 3; 4];
Making a multidimensional array:

>>x = [1 2; 3 4];  
>>x(:,:,1) = [5 6; 7 8];

Referring to an index outside of the current size will expand the array to fit the values. All values between the one specified and the original size will be set to zero

Additionally you can initialize an array to a certain range
>>x = 1:9;
>>x = 1:2:9 here the middle number is the step

More functions with arrays are available on page 5

Function Handles

  • >>x= @functionName points to a function called functionName
  • Variables can be assigned functions
  • Function variables can be passed to other functions
  • functions can be called with the variable x(in1, in2)

Operators and Reserved Symbols

  • +,-,/,*,^ are standard mathematical operators
  • == tests for equality
  • ~= tests for inequality
  • >,<,<=,>= compare value
  • ~ is the logical "not"
  • || logical or with short-circuiting
  • && logical and with short-circuiting

When storing values of equality, they are either 1 (true) or 0 (false) as the class logical

  • % reperesents a comment
  • ! sends command to operating system

Commands to the operating system do not function well on macs, additionally it is not recommended to try messing with the OS on a school computer

%{  
 This is a multiline comment  
 No code will be executed between the braces  
%}
⚠️ **GitHub.com Fallback** ⚠️