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:

1 2 3 |
C = {ones(3) magic(2) 'minutify'; ... [0:9] 'Text' 10; ... 'Home Address' 'A' 20} |

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:

1 2 3 |
>> C{2,1} ans = 0 1 2 3 4 5 6 7 8 9 |

A practical example to use cell array is this:

1 2 3 4 |
x=0:0.1:5; y=sin(2*pi*x); p={'o-','color',[.5 .5 .1],'linewidth',2}; plot(x,y,p{:}) |

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:

1 |
axis off |

*Change axis range*

To change the the x and y range use:

1 |
axis([xstart xend ystart yend]); |

You also can use:

1 |
xlim([xstart xend]); |

to limit the range of x, and:

1 |
ylim([ystart yend]); |

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:

1 |
title('This is \int x^2 dx') |

*Changing font size*

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

1 |
set(gca,’fontsize’,14); |

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.

1 2 3 |
x=3*ones(100000,20); y=repmat(3,100000,20); z=zeros(100000,20);z(:)=3; |

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:

1 |
y([200:300],[8:10])=5; |

**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:

1 2 3 4 5 6 |
x=0:0.1:4; y=sin(x*pi); plot(x,y); grid on; set(gca,'xtick',[0 2 4]); set(gca,'xticklabel',{'zero','two','four'}) |

**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:

1 2 3 4 |
for n=1:100 eval(sprintf('load f%d;',n)); process the data; end |

**Creating multi-line title**

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

1 |
title({'This is the first line','This is the second line', 'And this is the 3rd!'}); |

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. **

Pingback: William LeGate

Pingback: video intros

Pingback: agen baccarat online

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