matlab如何更改程序中所有同名_如何使用与用户定义函数同名的MATLAB工具箱函数...

I am having a problem with the findpeaks function, this function is in the signal processing toolbox and also the program has another version of it (user defined function). I need to call the on in the signal processing toolbox not the user defined one, also I can't rename the user defined function for many reasons. Can anyone help me in calling the toolbox function.

解决方案

The precedence order used by MATLAB is described in their help pages. It states that functions in the current folder (9.) are preferred over functions elsewhere in the path (10.). Then, the first appearance of the function in the path is chosen. This allows for a number of possible solutions:

1. cd to folder

A very simple method is simply to change the current workspace directory to the folder of the function you need to call, i.e. cd either to the place where your user-defined function is, or cd to the toolbox path. Note: This is rather inelegant, but probably sometimes the simplest solution.

2. Reorder path

As mentioned, MATLAB choses the first occurence of the function in the path. You can thus re-sort the path variable, so the folder where your user-defined function is, appears last. The path variable can be viewed and manipulated using the path function. Note: Then you can only call the toolbox function. Otherwise you'd have to resort the path again.

3. Function handles

If you need to be able to call both functions, it can be useful to create a function handle for both versions. For that, you have to cd into the folders where the functions are defined and create a new handle there:

cd('path/to/userdefined/function')

userFindPeaks = @findpeaks;

cd('path/to/MATLAB/installation/toolbox/signal/signal')

toolboxFindPeaks = @findpeaks;

You can then call the functions using feval.

Of course, as Adriaan mentions in the comments, it is best not to use the names of already defined functions for your own functions or for variable names.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值