Matlab Tutorial 7: Common Programming Structures and Conditional Statements

In this tutorial we will deal with some common programming structures and also alternative conditionally statements. These statements exist as well in almost all high-level programming languages. In Matlab a common programming construction is a if or a switch statement. To repeat a number of statements, can be solved with a for or a while loop.
In the following examples we will see how this is implemented in Matlab, but nevertheless how good programmers we are, some mistakes will happen. This also motivates a brief view of the debugger in Matlab.

For Loop in MATLAB

The general expression for a for-loop is:

for variable=expression
control statements

Example 1:

Let us write an m-file that calculates the sum: The sum to be calculated is: 1+1/22+1/32+…..+1/102.

for i=1:10
fprintf('The sum of 10 elements gives: %f ', the_sum)

The execution of the code in Matlab command window gives: The sum of elements gives: 1.549768
Read more »

Matlab Tutorial 4: Data Analysis and Statistics with Matlab

This tutorial covers data analysis and statistics using Matlab.

Histogram Charts in Matlab

The elements of a vector can be displayed with bars or histograms. To create a histogram you need to divide the elements in to classes and count how many elements that belongs to each class. Then present them as rectangular bars in a diagram. The height of the rectangle is equal to the number of elements in that class. Read the vector.

Matlab Histogram Example

Matlab Histogram Example

  • hist(x) Plots a histogram with 10 intervals (default) for the elements in vector x.
  • hist(x,n) Plots a histogram with n intervals for the elements in vector x.
  • hist(x,y) Plots a histogram with arbitrary intervals. These are given in vector x.

Introduce a histogram with 15 intervals for the vector x above. It is rather difficult to see how long the intervals are. Maybe its better to introduce a histogram with 6 intervals since we know the difference between the maximum and minimum value.

>> hist(x,6)

If we don’t know the dataset, we can define the intervals that we are interested to have in the histogram. Suppose we want the integer values between 0 and 10.

>> y=0:10;
>> bar(x),grid
>> title('bar for vector x')

Histogram Bars with Grid in the Background

Histogram Bars with Grid in the Background

Read more »

Matlab Tutorial 3: Strings in Matlab

The concept of variables is fundamental in all programming. In Matlab you introduce variables and assign values to them all by yourself. When you assign a value to a variable its previously assigned value will be overwritten. You can at any time find out the actual value of a variable simply by writing the variable name in the command window (without ;).

The class is decided by the assigned value.

>> a=1; % the class is double and the storage size is 8 bytes.

Open and check workspace.

In this laboratory we will investigate strings and conversions of strings among other things. A string is stored as a row vector, so that every element represents a character. Each character is accessible by the name of the vector and its indice.

Exercise 1: Assigning a variable to a string of characters

>> words='This is a string'

Press on enter to see the content of the variable words. Words has the size 1×16 elements. The class is char (character) and storage size 32 bytes.

>> words(2) % gives the second element in the variable words.
ans = h
>> words(2)='t' % replaces the second element in the vector with t.

Sooner or later we need some form of interactivity. For instance to get input from the user. To prompt the user for an input:
Read more »

Matlab Tutorial 2: Matrices in Matlab

Matrices in Matlab

In the previous tutorial we have used the concept vector. This is a special case of matrix. A two-dimensional matrix is nothing but a rectangular table with its elements ordered in rows and columns. A matrix mxn consists of m rows and n columns. In Matlab this can be written for a matrix A.

>> C=[ x' y1' y2']
C(:,1) % : means all rows and 1 stands for the first column.
C(2,:) % means the second row and all columns.

This matrix A has 2 rows and 3 columns. The first row is: 1 2 3 and the second: 4 5 6 . For the columns we have the have following order: Column 1: 1,4 column 2: 2,5 and finally column 3: 3,6

Each entry in the matrix A is accessible by using the following indices:

>> plot(C(:,1), C(:,2),C(:,1),C(:,3)), grid

For instance try the following :

>> ones(3) % Gives a quadratic matrix, with three columns and three rows
% only containing ones.


>> ones(3,5) % Same as above, but with three rows and five columns.

The most common matrix in matlab is the two-dimensional one. Many of the commands in matlab are only for valid for such matrices. The arithmetic operators (+, -, *, / and ^) that we used in tutorial1 can also be applied for matrices, but we also have some others as well.

Exercise 1: Vectors in Matlab

Generate a vector x=[5, -4, 6 ] with three elements. In Matlab as:

>> zeros(2) % Gives a quadratic matrix 2X2, and with zero as elements.

or alternatively

>> eye(3) % Gives the unity matrix with ones on the main diagonal.

What is the answer of x(4) and x(0) ?

The indices in a vector starts from 1 and in this case ends with 3. Therefore to ask for x(4) and x(0) is pointless. Suppose we would have done differently creating the vector x. Read more »

Matlab Tutorial 1: Hello world, plotting, mathematical functions and file types

In this first Matlab tutorial, I will try to show you the basics of Matlab user interface, data types, simple functions and mathematical operations. All with basic examples so that anyone with any level of programming knowledge can start using Matlab as a mathematical laboratory.

Matlab User Interface

Start Matlab by a double click on the Matlab icon or else by searching for it under program. Now, there should be a large window containing several smaller. These could for instance be:

  • Command Window
  • Command History
  • and Workspace.

This will of course depend on what version you are currently working in. This is the desktop of Matlab.

Command Window: Here you can write your own command lines and access your own files (m-files), but normally you can also see the output from the calculations in this window.
Command History: All command lines are saved here and can be seen in the window, but the same can be achieved by using the arrow button (up). The past command lines can the be seen in the command window.
Workspace: The variables that have been used or created during the execution will be shown in this window. Here you can see value, bytes and class. When you double click on the variables, the elements of the variables become visible.

These three windows should be the default when you start Matlab. Read more »