Concepts for MatLab
MATLAB is a high-level programming language primarily used for numerical computing and data analysis. Developed by MathWorks, it provides a powerful environment for matrix manipulation, algorithm development, and visualization. MATLAB's syntax is optimized for mathematical operations, making it particularly suitable for engineers, scientists, and researchers working in fields such as signal processing, image processing, control systems, and machine learning. Key features include built-in functions for linear algebra, statistical analysis, and plotting, as well as an extensive library of toolboxes for specialized applications. MATLAB's interactive environment, combined with its wide range of functionalities, makes it a popular choice for prototyping algorithms and conducting scientific research.
Print Text
disp('Hello World!'); fprintf('Hello World!');Comments
% one line comment %{ multi line comment %}Variables
carCount = 2; name = 'Joe'; $isPurple = true;
Data Types
carCount = 2; age = 15; seaLevel = -10000; usBudget = 11000000000; lightSpeed = 11799312874; myGPA = 3.45; piValue = 3.14159265; lastLetter = 'Z'; isMale = true; myName = 'Joe';
Math Op
carCount = 6; truckCount = 3; total = carCount + truckCount; disp(total); subtract = carCount - truckCount; disp(subtract); multiple = carCount .* truckCount; disp(multiple); divisble = carCount ./ truckCount; disp(divisble); % performs division and gets remainder which is 0 modulo = mod(carCount, truckCount); disp(modulo);
Math Functions
power = 3 .^ 6; disp(power); root = sqrt(16); disp(root); floorVal = floor(3.14); disp(floorVal); ceilVal = ceil(4.3); disp(ceilVal); roundVal = round(3.4); disp(roundVal); % random number from 0 to 100 ranNum = randi([0, 100]); disp(ranNum);
If/Else
carCount = 6; if carCount > 3 display('More than three cars'); elseif carCount == 3 display('Equal to three cars'); else display('Less than three cars'); endTernary
// No Native Support or Implementation
Switch
carCount = 3; switch carCount case 1 disp('One car'); case 2 disp('Two cars'); case 3 disp('Three cars'); otherwise disp('More than three'); endFunctions
function result = handleCars(cars, wheels) result = cars.* wheels; end // command line result = handleCars(3, 4)
Interpolation
name = "Joe"; car = "Mazda"; result = sprintf("%s drives a %s", name, car); display(result);Type Casting
function area = squareA(length) result = length.^2; area = int2str(result); end // command line squareL = "4"; convertedVal = str2num(squareL); area = squareA(convertedVal); display(area);
Date and Time
% milliseconds since 1970 milliseconds = posixtime(datetime('now')); disp(milliseconds); % format current time now = datetime; disp(now); % current year now.Format = 'yyyy'; disp(now); % current month now.Format = 'MM'; disp(now); % formatted date now.Format = "MM/dd/yyyy"; disp(now);Classes
classdef Cars < handle properties carList = [] end methods function obj = addCar(obj, car) obj.carList = [obj.carList , car]; end function obj = removeFirst(obj) obj.carList (1) = []; end function firstCar = getFirstVal(obj) firstCar = obj.carList(1); end end end // command line newList = Cars newList.addCar("Honda"); newList.addCar("Mazda"); newList.addCar("Toyota"); newList.removeFirst(); firstCar = getFirstVal(newList); disp(firstCar);Inheritance
classdef User properties username password end end classdef Admin < User properties id end end // command line newAdmin = Admin; newAdmin.id = "1FE"; newAdmin.username = "alpha0"; newAdmin.password = "pw1"; disp(newAdmin.username); disp(newAdmin.id);Method Overload
// No Native Support or Implementation
Abstract Class
classdef Animation methods(Abstract) animate_1 = walk(obj); animate_2 = run(obj); animate_3 = idle(obj); end end classdef Human < Animation methods function walk(obj) disp("Human walks"); end function run(obj) disp("Human runs"); end function idle(obj) disp("Human idles"); end end end classdef Zombie < Animation methods function walk(obj) disp("Zombie walks"); end function run(obj) disp("Zombie runs"); end function idle(obj) disp("Zombie idles"); end end end player = Human; player.walk; boss1 = Zombie; boss1.run;Static Class
classdef Helper methods(Static) function area = CircleA(radius) area = (radius.^2) * 3.14; end function area = SquareA(length) area = length.^2; end end end circle = Helper.CircleA(3); disp(circle); square = Helper.SquareA(4); disp(square);Arrays/Lists
names = ["Joe", "Alex", "Bob"]; names(2) = "Tom"; name = names(2); numItems = size(names); disp(name); disp(numItems);
Array Methods
cars = ["Honda", "Mazda", "Toyota"]; % number of elements disp(numel(cars)); % checks to see that array is not empty disp(isempty(cars)); % last elment of array disp(cars(end)); % adds Ford to end of cars cars = [cars, "Ford"]; % sorts the array cars = sort(cars); disp(cars); % removes the last element cars(1)=[]; disp(cars(numel(cars) - 1)); % remove the first element cars(end)=[]; disp(cars(1));
Concatenation
carsListKr = ["Kia", "Hyundai", "Daewoo"]; carsListJp = ["Honda", "Mazda", "Toyota"]; combined = horzcat(carsListKr, carsListJp); disp(combined(2)); disp(combined(5));
Sort Method
ages = [5, 3, 1, 6, 7, 4, 19]; result = sort(ages); disp(result);
Objects
classdef User properties first; last; age {mustBeNumeric}; retired {mustBeNumericOrLogical}; carBrands; end methods function result = fullName(obj) result = sprintf("%s %s", obj.first, obj.last); end end end // command line brands = ["Mazda", "Toyota"]; user = User (); user.first = "Joe"; user.last = "Doe"; user.age = 23; user.retired = false; user.carBrands = brands; disp(user.first); disp(user.carBrands(1)); disp(user.fullName());Maps (Key/Value)
carMap = dictionary(); carMap("Joe") = "Toyota"; carMap("Bob") = "Mazda"; carMap("Tom") = "Ford"; disp(carMap("Tom")) carMap = remove(carMap, "Tom"); disp(isKey(carMap, "Tom"))Sets
# no built in implementation # use external libraries
Stack
cars = []; cars = [cars, "Mazda"]; cars = [cars, "Toyota"]; cars = [cars, "Honda"]; fprintf(cars(end)); cars(end)=[]; fprintf(cars(end));
Queues
cars = []; cars = [cars, "Mazda"]; cars = [cars, "Toyota"]; cars = [cars, "Honda"]; fprintf(cars(1)); cars(1)=[]; fprintf(cars(1));
Linked List
% keep classes in its own files classdef Node < handle properties value next end methods function obj = Node(Value) obj.value = Value; obj.next = {}; end end end classdef LinkedList < handle properties head = {} end methods function obj = add(obj, value) newNode = Node(value); if isempty(obj.head) obj.head = newNode; else current = obj.head; while isempty(current.next) == 0 current = current.next; end current.next = newNode; end end function head = returnHead(obj) head = obj.head.value; end function traverse(obj) current = obj.head; while isempty(current) == 0 disp(current.value); current = current.next; end end end end carList = LinkedList(); obj = carList.add("Mazda"); obj = carList.add("Toyota"); obj = carList.add("Honda"); head = carList.returnHead(); disp(head); carList.traverse();Graphs
classdef Graph properties graphMap end methods function obj = Graph() obj.graphMap = containers.Map(); end function addNode(obj, val) obj.graphMap(val) = {}; end function addVertices(obj, node1, node2) if ~isKey(obj.graphMap, node1) obj.graphMap(node1) = {}; end if ~isKey(obj.graphMap, node2) obj.graphMap(node2) = {}; end obj.graphMap(node1) = [ obj.graphMap(node1), node2 ]; obj.graphMap(node2) = [ obj.graphMap(node2), node1 ]; end function breadthTraverse(obj, start) queue = {start}; visited = containers.Map(start, true); while ~isempty(queue) val = queue{1}; disp(val); queue(1) = []; tempArr = obj.graphMap(val); for i = 1:length(tempArr) if ~visited.isKey(tempArr{i}) queue = [queue, tempArr{i}]; visited(tempArr{i}) = true; end end end end function depthTraverse(obj, start) stack = {start}; visited = containers.Map(start, true); while ~isempty(stack) val = stack{end}; disp(val); stack(end) = []; tempArr = obj.graphMap(val); for i = 1:length(tempArr) if ~visited.isKey(tempArr{i}) stack = [stack, tempArr{i}]; visited(tempArr{i}) = true; end end end end end end newGraph = Graph(); newGraph.addNode('a'); newGraph.addVertices('a', 'b'); newGraph.addVertices('a', 'c'); newGraph.addVertices('b', 'd'); newGraph.addVertices('b', 'e'); newGraph.addVertices('d', 'f'); newGraph.addVertices('d', 'g'); newGraph.addVertices('d', 'h'); newGraph.addVertices('e', 'i'); newGraph.addVertices('e', 'j'); newGraph.breadthTraverse('a'); newGraph.depthTraverse('a');For Loops
names = ["Joe", "Alex", "Bob"]; for i = 1:length(names) fprintf(names{i} + "\n"); endWhile Loops
carList = ["Mazda", "Toyota", "Honda"]; while size(carList) > 0 fprintf(carList(1)) carList(1)=[]; end disp(isempty(carList));
Loop Breaks
cars = {"Toyota", "Honda", "Mazda"}; carCells = cellstr(cars); for i = 1:length(carCells) if carCells(i) == "Honda" result = sprintf("Fount at index %d", i); fprintf(result); break; end result = sprintf("Visited index %d", i); fprintf(result); fprintf("\n"); endRecursion
function result = getSum(arr) if 1:length(arr) == 1; result = arr(1) else val = arr(1); arr(1)=[]; secVal = getSum(arr) result = val + secVal; end end testArr = [1, 2, 4, 5]; result = getSum(testArr); disp(result);Regex Words
phrase = "I am legend"; pattern = regexpPattern('[a-zA-Z]+'); result = extract(phrase, pattern); % only first result disp(result(1));Regex Numbers
phrase = "I am 23"; pattern = regexpPattern('[0-9]+'); result = extract(phrase, pattern); % only first result disp(result(1));Regex Test
phrase = "Bob Lee"; pattern = "Lee"; result = ~isempty( ... regexp(phrase, pattern) ... ) && 1; % only first result disp(result);
Regex Characters
phrase = "##I am legend##"; pattern = regexpPattern('\#+[a-zA-Z]'); result = extract(phrase, pattern); % only first result disp(result(1));