Skip to content

Instantly share code, notes, and snippets.

@jenncross
Last active April 22, 2019 16:13
Show Gist options
  • Save jenncross/43e94df0dfb7ba2fe8cf0af45b7e3ab7 to your computer and use it in GitHub Desktop.
Save jenncross/43e94df0dfb7ba2fe8cf0af45b7e3ab7 to your computer and use it in GitHub Desktop.
Example primer for MATLAB from python
%Get started by opening MATLAB and clicking "New Script" then paste this code in the script window.
% Run the code by clicking "Run" in the editor tab.
%In MATLAB, comments are marked with percent signs instead of # (hash signs) or """
%(triple quotes).
%Code that is saved in a file and performs some actions is called a script.
%MATLAB scripts are saved with the suffix .m
%Assignment is still done with an equals sign and linspace works basically
%the same as with numpy.
a = linspace(0,10,21)
%We do not need to import libraries like math or numpy as those are
%built-in to basic MATLAB.
% b = sqrt(a) %remove the first percent sign to uncomment this code.
%Also when this code is ran, EVERY LINE prints it's output, unless you use
%a semi-colon to prevent printing. Like so...
% c = b + 1; %uncomment me!
%Unlike Python, MATLAB starts indexing at 1, this is a hotly debated point.
%Indexing at one matches English better, for example "first element" will
%correspond with the number 1 but index with 0 makes many calculations
%easier.
%Also indexing is done with parentheses not brackets.
% d = a(1:4) %uncomment me!
%We can also do type casting on arrays, however, the names are a little
%different. For example, "string" is used instead of "str"
% e = string(d) %uncomment me!
%If you want to display text (beyond the automatic printing), we use disp
%instead of print.
% disp("Hello") %uncomment me!
% For loops are similar, but the equals sign replaces the key word "in"
% from Python and there is no colon. (for example, here we have the equivalent of "for n in e")
% Notice that the for loop is concluded with an "end" keyword. "end" is
% also required for if statements.
% for n = e %uncomment me!
% disp(n)
% end
%And + sign stills adds numbers and concatenates strings
% for i = d %uncomment me!
% s = string(i);
% disp("S is " + s)
% disp(i+i)
% disp(s+s)
% disp("-----")
% end
%The MATPLOTLIB library is actually based on the plotting features of MATLAB. So you might
%feel very familiar with it already.
%plot(b, "ko") %uncomment me!
% But if you ever need help you can also, type help and the name of the function you want
% to learn aboutinto the command window.
%help plot %uncomment me!
%Custom functions in MATLAB are stored with a single function for each function file. The
%function files also have a .m suffix. These files start with a line like:
%function [out1,out2, ..., outN] = myfun(in1,in2,in3, ..., inN) where the
%** out1, out2, etc. are the outputs -- e.g. the return values
%** myfun is the name of the function, this should also be the name of the
%file (for instance myfun.m)
%** in1, in2, etc. are the arguments that the function takes.
% This custom function can then be used in any .m script that is in the
% same folder as that function file. (i.e. in the working directory).
% The following would run that myfun function, if it had 5 inputs.
%result = myfun(2, 3, 5, 1, 4) %uncomment me!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment