Is there a way to use my own function file for a Matlab function?
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
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:
license('test','Image_Toolbox')
More Answers (2)
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. https://www.mathworks.com/help/matlab/matlab_prog/private-functions.html
0 Comments
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')
end
chol(2)
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.4142
See Also
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!