Tips for Using Custom Functions in Matlab

When using Matlab, many users gradually accumulate their own “custom functions”. However, as the number of custom functions increases, or for those who frequently move code scripts, you may find that: the script location of the custom functions is not in the same path as the running script, which will result in an error when executed.How can this issue be resolved?Some may choose to copy the script of the custom function to the current running script location, which is a valid approach.However, as projects become more complex and the number of custom functions increases, or when a custom function is used in multiple scripts, the aforementioned method can become quite “cumbersome”.Instead, let’s consider my approach:Organize your existing custom functions and save them in a dedicated path. Of course, you can also categorize and save different custom functions based on your needs.Tips for Using Custom Functions in MatlabHow to use it?All you need to do is add a line of code in the current running script:

addpath("path_to_custom_functions")

which adds the path of the custom functions.Reminder: If you are accustomed to storing code and other files on a portable hard drive, make sure to confirm the drive name before using the above method. This will help avoid issues where the computer automatically assigns the portable hard drive as D: or F:, causing the “path to custom functions” to become invalid.

Leave a Comment