2

On my Matlab path there's a custom zeros function. I want to store a handle to the built-in zeros in a variable. How can I do that?

Thought about @(varargin)builtin('zeros',varargin{:}), but this would probably slow down the operation due to the string comparison.

Also, I've noticed that it's possible to refer to diag as @numel\diag, but this doesn't seem to work with other built-in functions (zeros in particular).

Amro
  • 123,847
  • 25
  • 243
  • 454
Michael Litvin
  • 3,976
  • 1
  • 34
  • 40
  • What would be the purpose of doing this? Why not call your custom function something different and save yourself the trouble? – MZimmerman6 Aug 13 '13 at 11:40
  • possible duplicate, but not exactly the same: [this](http://stackoverflow.com/questions/11781634/how-to-wrap-an-already-existing-function-with-a-new-function-of-the-same-name) and [this](http://stackoverflow.com/questions/17727141/how-to-select-a-specific-m-function-when-two-exist) – Gunther Struyf Aug 13 '13 at 12:33
  • What do you mean with 'slow down the operation due to string comparison'?? They're is just one extra step by using `builtin` and that's the function `builtin` itself, the argument expansion imo takes no extra steps – Gunther Struyf Aug 13 '13 at 12:39
  • @MZimmerman6, the purpose is to create a wrapper that chooses between the builtin zeros and gpuArray.zeros. – Michael Litvin Aug 13 '13 at 15:18
  • @GuntherStruyf, by slow down i mean that it would impose a string comparison an additional function call every time I call zeros. – Michael Litvin Aug 13 '13 at 15:18
  • @MichaelLitvin That string comparison can't be that bad. It will only add a few millisecond at max – MZimmerman6 Aug 13 '13 at 15:20
  • Yeah that could be a solution, but there's no need. It's not a duplicate as here I was looking for the handle, and there just to call the function. – Michael Litvin Aug 13 '13 at 15:23

2 Answers2

4

Suggestion #1

% At the beginning of your script:
rmpath('C:\the\folder\containing\the\custom\zeros');
builtInZeros = @zeros;
addpath('C:\the\folder\containing\the\custom\zeros');

% Calling the custom zeros later:
a = zeros(10, 20);

% Calling the built-in zeros:
b = builtInZeros(10, 20);

Suggestion #2

Put these three lines into your startup file:

rmpath('C:\the\folder\containing\the\custom\zeros');
builtInZeros = @zeros;
addpath('C:\the\folder\containing\the\custom\zeros');

Suggestion #3

It's definitely a dangerous idea to reuse the name of a built-in function. It ruins the readability of your scripts, making them much more difficult to maintain. So if you have control over the custom zeros function, then rename it to something else. Use a name which describes how the custom version is different from the built-in one (for example, call it fastZeros if it's faster).

kol
  • 27,881
  • 12
  • 83
  • 120
2

Well, this doesn't give you an exact answer to your question, but it could solve the problem:

I think this seems to be a good solution:

matlabcentral: How to call a shadowed function

Withn the last post:

Just stumbled upon this problem and found the following solution: For example, I have matlab svmtrain shadowed by libsvm toolbox:

which svmtrain -all

C:\Projects\Ichilov\Misc\MVPA\libsvm-mat-3.0-1\svmtrain.mexw64

C:\Program Files\MATLAB\R2009b\toolbox\bioinfo\biolearning\svmtrain.m % Shadowed

But I can access the original function by using str2func:

org_svmtrain = str2func([matlabroot '\toolbox\bioinfo\biolearning\svmtrain'])

and then simply calling:

org_svmtrain(training, groupnames)

Lucius II.
  • 1,832
  • 12
  • 19