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(‘https://www.alivelearn.net’). 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’)



写作助手,把中式英语变成专业英文


Want to receive new post notification? 有新文章通知我

采用基于频率簇(Cluster)的置换检验(Permutation)方法选取感兴趣频段

作者:北京师范大学 龙宇航,[email protected]代码来源(见本页底部):周思远 在使用wtc计算脑间神经同步后,我们需要在多个频率段、多个通道组合上对神经同步值进行统计检验,因
Xu Cui
1 min read

Calculate phase difference between two general signals (e.g. HbO…

In a recent fNIRS journal club (vedio recorded here), Dr. Tong talked about their work on the phase difference between oxy and deoxy Hb, and its relationship with participants’ age. This article is a demo of how to use Hilbert transform to calc
Xu Cui
1 min read

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 is. The download link is at the bottom of this article. nirs2img is to create an image file from the input dat
Xu Cui
51 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 *