matlab - RicoJia/notes GitHub Wiki
========================================================================
========================================================================
-
Cell
- a cell is similar to a matrix, but can contain any datatype.
cell{1}constructs a cell with value 1.
- a cell is similar to a matrix, but can contain any datatype.
-
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 -
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.
-
-
By default, classes have value behavior.
-
Set Path of the currrent directory.
- Online MATLAB should use pathtool to add path. Then, add_with_subfolders.
========================================================================
========================================================================
-
How to run program: step, or run
-
ginput()gives you a set of points from screen -
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
-
polyvalevaluate a polynomial (Pnxnn<\sup> + ...) decending powerspolyval([], num)flipud(list): reverse it
-
function definitions are behind the script
-
help zeros: help -
indentation in matlab doesn't matter
-
clcclean screen -
;suppress command line inputs -
nested func: all variables are the same here
function foo() x = 100; function bar() x = 99; end end
-
function return, must be like
function [var] = something() -
if -else -elif
if else elseif end
-
nargin: number of arguments
- ^ is matrix power, .^ is element power
-
isemptyfor checking arrays -
rem(a,b)for checking remainders -
nchoosek(n, k),c_n^k -
addpath ../to add another dir -
length(arr): length of the array,- similar to
size(arr, ndim)
- similar to
-
rand: uniform(0, 1) pi-
x(x<0)=0if x<0, x=... - Mat can be flattened out into 1d
M = [1,2 3,4] M(1) = 1 M(2) = 3
========================================================================
========================================================================
- matlab no tiene
+= - Definir variables globales es difficil, atraves de los archivos
-
arr(1:2) = [1;2]es valido