Introduction to MATLAB

  1. The Matlab Environment

  2. MATLAB®   (matrix laboratory)  is a high-level language and interactive environment that allows matrix manipulations, plotting of functions and data, implementation of algorithms, creation of user interfaces, and interfacing with programs written in other languages, including C, C++, Java, and Fortran.

    You can use MATLAB in a wide range of applications, including signal and image processing, communications, control design, test and measurement, financial modeling and analysis, and computational biology.

    There are add-on toolboxes (collections of special-purpose MATLAB functions) extend the MATLAB environment to solve particular classes of problems in these application areas.

    • Buy the official ones from MathWorks ,
    • Download some good packages for free from the web or matlab users community (see here).

    MATLAB is an interactive computing environment, you may enter expressions and obtain an immediate evaluation:

    >> 2+2

    and you will get ans (answer).

    ans =
  3. The Interface

  4. MATLAB interface is composed by

    • COMMAND WINDOW, where you enter commands, run MATLAB functions and scripts (m-files). MATLAB here displays the results.
    • COMMAND HISTORY, the statements you enter in the Command Window are logged with a timestamp in the Command History.
    • CURRENT FOLDER, it displays details about files in your current folder and within the hierarchy of the folders it contains.
    • WORKSPACE, it isplays the Workspace browser, a graphical user interface that allows you to view and manage the contents of the workspace in MATLAB.

  5. The Help
  6. Your best friend in Matlab is definitely the Help, you can use the Help GUI from HELP->Product Help or search on it with a shortcut from the COMMAND WINDOW  docsearch keyword1  keyword2.
    Alternatively, you can have the function specific features by using help from the COMMAND WINDOW with help function name or help toolbox .

    For istance type,

    >> help cov

    and you will get

    >> help cov
     COV Covariance matrix.
        COV(X), if X is a vector, returns the variance.  For matrices,
        where each row is an observation, and each column a variable,
        COV(X) is the covariance matrix.  DIAG(COV(X)) is a vector of
        variances for each column, and SQRT(DIAG(COV(X))) is a vector
        of standard deviations. COV(X,Y), where X and Y are matrices with
        the same number of elements, is equivalent to COV([X(:) Y(:)]).
        COV(X) or COV(X,Y) normalizes by (N-1) if N>1, where N is the number of
        observations.  This makes COV(X) the best unbiased estimate of the
        covariance matrix if the observations are from a normal distribution.
        For N=1, COV normalizes by N.
        COV(X,1) or COV(X,Y,1) normalizes by N and produces the second
        moment matrix of the observations about their mean.  COV(X,Y,0) is
        the same as COV(X,Y) and COV(X,0) is the same as COV(X).
        The mean is removed from each column before calculating the
        Class support for inputs X,Y:
           float: double, single
        See also corrcoef, var, std, mean.
        Reference page in Help browser
           doc cov
  7. The Editor – where we code

  8. It provides a graphical user interface in order to create and debug programs that we write.
    To create or edit a file select File > New or File > Open, or use the edit function “edit namefile“.
    Colors highlight various MATLAB language elements — blue for keywords, green for comments.
    The code analysis message bar contains an orange box and bar, indicating there are areas for improvement in the code.

    M-files contains the commands that MATLAB will execute. When you are naming M-files remember that a file must begin with a letter and it can include both letters, numbers and underscores. In order to avoid function conflicts you should avoid reserved words (if, else, for, end, while, …) and existing function names (mean, var, cov, sum, corr, …).
    If you want to check whether a name is already in use, the command “which filename” will eventually list files by the same name. Let’s try it on the COMMAND WINDOW.

    >> which mean

    and we get

    >> which mean

    When placed at the end of a command, the semicolon tells MATLAB not to display any output from that command.
    In this example, MATLAB does not display the resulting 100-by-100 matrix,

    A = ones(100, 100);

    Comments are a good practice of programmers, they allow to track what you do on the code ( especially when you handle the code again after long time).
    Use % symbol to comment and MATLAB will sto processing anything on that line to the right of the symbol and will restart the next line.
    %% This is a comment.

  9. …(dot-dot-dot)

  10. If we are coding an expression that cannot be easily rappresented in a single line we can continue it in the next line.

    x = 10;
    y = 5;
    x = x + 3*x + y + x*y + exp(x) / log(x) * sqrt(2*x*y);

    This feature exists purely to improve the code readability.

    x = 10;
    y = 5;
    x = x + 3*x + y + x*y...
    + exp(x) / log(x) * sqrt(2*x*y);

  11. Set Path

  12. If we want to use “globally” some packages, routines, functions or particular m-files we have to add it to the MATLAB search path, with “pathtool” from COMMAND WINDOW or by selecting FILE > Set Path

    … and the clic to “Add Folder…” or “Add with Subfolders…” if you have secondary folders with m-files.


    MATLAB has very good demos born to explain its potential. Take a look!


    Some usefull exercises…

    1. Become familiar with MATLAB COMMAND WINDOW.
    2. Read the section MATLAB, Getting Started, Introduction in the Help Browser.
    3. Launch the editor and explore its interface.
    4. Play some demos.
%d bloggers like this: