MATLAB tips and tricks: part 2

This is the second part of our "MATLAB tips and tricks" series. As we also mentioned in part 1, MATLAB is the programming language of choice for many engineers, scientists, data analysts, and more. In this post we introduce some interesting MATLAB tips and tricks. They can be used to make writing code easier and shorter, also to write codes that run faster. Since there are too many MATLAB tips and tricks, we will break this topic into a number of posts. We try to keep the number of tips to less than 10 in each post so it can be read in a few minutes. Without further ado let's jump right in.

Cell Arrays

Cell arrays are great because they can store elements with different sizes and types in one unit. For example you can have an array with different cells as follows:

To access each element (cell) of this array we use {}. For example to access the cell on row 2, column 1 we use C{2,1}. If we type that in MATLAB workspace we get this:

A practical example to use cell array is this:

Please note that in the above example p{:} is equivalent to the comma separated list of cells in p, i.e. p{:} = "p{1}, {p2},...".

Maniplating axis in a figure

There are many cases that we like to change some of the properties of axis in a MATLAB figure. Here are some interesting examples.

Don't show axis
To turn off axis of a figure just type:

Change axis range
To change the the x and y range use:

You also can use:

to limit the range of x, and:

to limit the range of y.

Using Latex
You can use latex commands to write mathematical equations in the texts of a figure. For example:

Changing font size
You can change the font size of the axis easily, for example:

sets the font size to 14.

Initializing an array with a constant

To initialize a large array with a constant, simply avoid for loops. Here are a number of methods that can do this very fast.

all of the above methods quickly initialize all the elements of vectors x, y, and z with a size of 100,000x20 to value of 3.
If you want to initialize or change just a range of elements you can do that as well. For example you can replace elements in rows 200 to 300 and columns 8 to 10 of y with 5 as:

Changing tickmarks and their labels

You can use xtick, ytick, xticklabel, yticklabel properties of a figure to control tickmarks and their labels.
Here are some examples:

and here is the plot:
xtick

Using sprintf and eval

You can use sprintf and eval for repetitive tasks. For example assume that you have 100 files with names f1.mat, f2.mat, ..., f100.mat. If you want to load each file and perform a task on the file content, you can either load each file and process manually, or you can use a for loop and do this:

Creating multi-line title

Here is an example that shows how to create a multi-line title using cell array:

References:
1. Matlab, Third Edition: A Practical Introduction to Programming and Problem Solving
2. MATLAB: An Introduction with Applications

We hope you find these MATLAB tips and tricks useful. If you know of other great tips and tricks we love to hear from you and possibly include them in our next post on this topic. Please send your tips to the email address provided on the "Contact" page.

If you have enjoyed reading this post, please subscribe to Minutify, so we can send you an email each time a new post is available.

4 thoughts on “MATLAB tips and tricks: part 2

  1. Pingback: William LeGate

  2. Pingback: video intros

  3. Pingback: agen baccarat online

  4. Pingback: https://www.youtube.com/watch?v=eC0-A3m2fMo&index=3&list=PLwuCkRbQ6BEWjoMREa5e19Ru7hD4lOXy2

Comments are closed.