Small MatLab tips

1 min read

  1. genpath
    When you add a folder to path, you often want to add all its subfolders (and their subfolders) to path too. In this case, you can use genpath. e.g. addpath(genpath(pwd))
  2. use ii, instead of i, in loop (suggested by Chess Stetson)
    You probably use the following code a lot: for i=1:100. The problem here is that i is special in MatLab. It stands for the imaginary number. So it’s a good idea to avoid it.
  3. urlread
    If you want to get some data from a webpage, you can use s = urlread(‘’). Then you can parse the returned string s.
  4. split a string
    You can use parts = regexp(tline,'\t','split'); to split a string into parts.
  5. Find common numbers (or strings) of two arrays (or cell arrays)
    You can use intersect(A,B) to find common numbers or strings.
  6. Do not use guide for big projects
    Guide is a quick way to build a graphic interface. But for bigger projects building the interface programmatically in the m file is more flexible. You have precise control of button positions, you can easily delete or add components and don’t worry about junk codes in your file. And you have only one file (the m file) instead of two files (fig file and m file).
  7. Try to avoid explicit loop.
    If you can use matrix operation, use it instead of looping. For example, if you want to multiply two vectors element by element and get the sum,  then use A*B’ instead of doing a loop. It is much faster.
  8. Allocate memory first for large arrays.
    If you know that the size of an array is one million, then allocate memory first (A = zeros(1, 1000000)), then fill numbers to each element. If you don’t allocate memory first, MatLab will keep allocate memory dynamically and the performance will be slow.
  9. evalin
    In your base workspace there is a variable called ‘x’. How to get the value of x? You can use evalin(‘base’,’x’).  Sometimes you may not explicitly know the variable’s name – you only know the variable’s name is stored in another variable called y, then you do evalin(‘base’, y).
  10. repmat
    If you want to make a larger matrix from a smaller one, you can use repmat. For example, B = repmat(‘this is a string’, 10, 1).
  11. system
    If you need to call system functions, you can use system. For example, system(‘dir’)

nirs2img, create an image file from NIRS data

Update 2021/2/27: If you find griddata3 not working, try to change griddata3 to griddata. I was asked where to get nirs2img script. Here it...
Xu Cui
51 sec read

mergefile.m – a MatLab script to merge CSV files

My wife asked me to write a script to merge some csv files she has. Usually this can be accomplished by a simple command in...
Xu Cui
35 sec read

xjview 9.6 released

In this version, we modified the templates for 3-D render view and use a high-resolution template. It also includes a few scalp view. You...
Xu Cui
31 sec read

4 Replies to “Small MatLab tips”

  1. Considering 9): in my opinion using ‘eval’ and ‘evalin’ is the worst thing you can do to your code. Both are based on concatenated strings and therefore very error-prone. Sometimes I use in the debug mode to compare data between the workspaces. In some other situations dynamic field names are useful to replace ‘eval’.

  2. @Alex

    You are absolutely right in that ‘eval’ will make the code less readable and hard to debug. However in some cases I can’t find other ways to do it. for example, in xjview program, I display a dialog which allows a user to select a variable in the workspace. After the user selects it xjview will read the value of that variable. Is there a workaround in this situation?

  3. In this case – for sure not.
    When mentioned ‘eval’ I was thinking about some peace of code I saw a year ago. Very bad stuff. Lines over lines executed with ‘eval’.
    By the way, considering 7): in some cases ‘arrayfun’ is very useful to avoid loops and to accelerate the code.

Leave a Reply

Your email address will not be published. Required fields are marked *