matlab - RicoJia/notes GitHub Wiki

========================================================================

Basics

========================================================================

  1. Cell

    • a cell is similar to a matrix, but can contain any datatype. cell{1} constructs a cell with value 1.
  2. function organization: foo can only be used in getScore, but not outside the file.

    1. funcs.m: 
    function g = getScore(input)
        g = foo(1)
    end
    
    function f = foo(input2)
        f = input2+1  
    end
    
  3. class organization:

    • Definition

      classdef My_Class
          properties  #(GetAccess='private', SetAccess='private', Constant)
              h
          end
          methods
              function obj = My_Class(input)      %%constructor
                  obj.h = 3
              end     
              function g = getScore(input)
                  g = foo(1)          %% definition of the func here
              end
          end
          methods (static)            %% static helper functions
              function g = staticMethod()
                  print lol
              end
          end
      end 
      
      function f = foo(input)
          f = f + 1
      end 
    • And in commandline window, you can set properties:

      s=My_Calss(input)   %% or simply s = My_Class if no inputs are required. 
      s.h = 123
    • use static methods without creating an object: My_Class.staticMethod()

    • handle class: inherit is <, matlab has built-in handle class that allows you to build a class that's used as a reference. So if your class is a big data structure, make it a handle class so functions do need to return it!! This can also be used in multiple workspaces.

    • events: like subscribers, it will trigger a callback.

    • func(@evCallback) %% you're passing a function in.

  4. By default, classes have value behavior.

  5. Set Path of the currrent directory.

    • Online MATLAB should use pathtool to add path. Then, add_with_subfolders.

========================================================================

Operations

========================================================================

  1. How to run program: step, or run

  2. ginput() gives you a set of points from screen

  3. matrix

    • a = [1, 2, 3; 4, 5, 6]
    • mat(1,1): how you access it, starting from 1
    • appending a new element: arr(1) = 3
    • last element: arr(some_index)
    • [mat1; mat2; mat3] is how we stack them
  4. polyval evaluate a polynomial (Pnxnn<\sup> + ...) decending powers polyval([], num)

    flipud(list): reverse it
  5. function definitions are behind the script

  6. help zeros: help

  7. indentation in matlab doesn't matter

  8. clc clean screen

  9. ; suppress command line inputs

  10. nested func: all variables are the same here

    function foo()
      x = 100; 
      function bar()
        x = 99;
      end
    end
  11. function return, must be like function [var] = something()

  12. if -else -elif

    if 
    else 
    elseif 
    end
  13. nargin: number of arguments

Operators and Commands

  1. ^ is matrix power, .^ is element power
  2. isempty for checking arrays
  3. rem(a,b) for checking remainders
  4. nchoosek(n, k),c_n^k
  5. addpath ../ to add another dir
  6. length(arr): length of the array,
    • similar to size(arr, ndim)
  7. rand: uniform (0, 1)
  8. pi
  9. x(x<0)=0 if x<0, x=...
  10. Mat can be flattened out into 1d
    M = [1,2
         3,4]
    M(1) = 1 
    M(2) = 3

========================================================================

Los que faltan en MATLAB

========================================================================

  1. matlab no tiene +=
  2. Definir variables globales es difficil, atraves de los archivos
  3. arr(1:2) = [1;2] es valido
⚠️ **GitHub.com Fallback** ⚠️