Is there a way to use my own function file for a Matlab function?

1 view (last 30 days)
Dear Matlab users,
I would like to know if it is possible to substitute a Matlab function with my own .m file? Why I want do to this: I process a lot of imaging data and I seem to run out of Image_Toolbox licenses sooner than Matlab licenses. The processing pipeline seems to use only `imerode` function from the toolbox, so I wrote my own function `own_imerode.m`. Luckily, it is quite straight-forward. However, I would like not to replace `imerode` with `own_imerode` throughout the code for two reasons - backward compatibility and hassle of going through the code. Instead, I was wondering if it is possible to tell Matlab to somehow use `own_imerode` when `imerode` is called, like an alias in a bash shell? I guess, one way would be to call my function also `imerode` and place the path to it on top of the list. Is there another way?
Best, Renat.

Accepted Answer

Andreas Goser
Andreas Goser on 12 Jan 2018
Renat, the root cause appears to be a lack of purchased toolboxes. Also you seem to process a lot of images. So I suggest you also look for accessing a cluster. You might have a MATLAB Distributed Computing Server license at your institution.
Beside that, I would not fiddle with the installation. You may want to a simple IF THEN ELSE statement at the beginning that looks for the toolbox being available and if yes use it and if not the alternative way. Command to use could be:
  1 Comment
Renat on 12 Jan 2018
Edited: Renat on 12 Jan 2018
I have access to an HPC cluster and this where I tun into the problem. It looks like after New Year the number of IPT licenses halved. So I looked into the processing pipeline code and realized that we don't really need IPT and I can use `conv2` instead.
I like the idea of checking for an available license and use my version if there is none. Thank you for the suggestion, I think I will use that!

Sign in to comment.

More Answers (2)

Walter Roberson
Walter Roberson on 11 Jan 2018
The defined route is to use the same function name and have the function earlier on your search path.
You can reduce the impact on other code by putting your imerode into a directory named private under the directory that calls the function.

John D'Errico
John D'Errico on 11 Jan 2018
Edited: John D'Errico on 11 Jan 2018
That is exactly how to solve the problem - just put it in a directory on your search path above the IPT directory.
Note that this also effectively disables the IPT imerode function, so if you ever need to use any capabilities of that tool that are not in your own code, it is no longer available to you. To restore access to the old imerode, you would need to change the search path, or delete or rename your version.
You may get warning messages from MATLAB, that your function steps on existing code. At least when you use it initially, but that warning seems to go away after the first use.
function chol(A)
disp('The sky is falling')
The sky is falling
One other way to temporarily recover use of the original tool is to use builtin, although I think that will only apply to actual builtin tools like chol. Toolbox functions seem not to be recognized by builtin.
L = builtin('chol',2)
L =
  1 Comment
Renat on 12 Jan 2018
The code uses the IPT imerode function to do an erosion using a line strel. This is very easily replaced with `conv2` function. I dont need its other capabilities.

Sign in to comment.


Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!