Modify built-in Matlab function without overwriting it

14 views (last 30 days)
For my current project I am using the insfilterNonholonomic object to fuse GNSS and IMU data. When looking at the code in detail (using the edit command), I realized that the motion model not only assumes motion without sideslip (as stated in documentation) but also no bouncing (not stated in documentation). I need a model that allows bouncing. This could be easily changed by removing one of the two velocity constraints (y and z direction), however I do not want to directly modify any built-in functions provided by Matlab since this will make it impossible to run the code on another computer. I am talking about adding two lines of code instead of having to re-write multiple thousand lines of code...
I understand that if I wanted to edit a simple function I could just save it under a different name and call it from my script using that name. However, the function that I need to change is called
correctKinematics(obj)
which is declared in the file NHConstrainedIMUGPSFuserBase.m on line 119 which is called in insfilterNonholonomic.m on line 371 using the command
obj@fusion.internal.NHConstrainedIMUGPSFuserBase(varargin{:});
I am using R2020b, I am not sure if maybe the line numbers changed in a newer release.
So how can I go about this if I don't want to rewrite all the code myself? Should I just manually copy all the involved files and "manually solve the dependencies"?
  2 Comments
John D'Errico
John D'Errico on 10 Nov 2021
Editing existing supplied code is a really, really bad idea. For any of the tools I have ever written, I always use the rule that if you change it, you own any problems that arise, in the sense that ANY problems that ever arise are now yours to deal with.
If you modify the code, then you want to make sure you modify it in a way that you copy and rename all such files, and then edit only your new versions. At least this way, if you trhen need to go back to the existing code, you can do so easily, and any others will not be surprised to see that code no longer works as expected.

Sign in to comment.

Accepted Answer

Jan
Jan on 11 Nov 2021
You want to runs the code on a different computer also. Then using a modified copy of the file an all its depenedencies is not a perfect solution also, because you have to ship these files also. Remember, that it Matlab's functions are covered by copy right, so distributing a toolbox function with 2 changed lines might be a legal problem.
Some years ago I've written an enhancement for filtfilt, which modified some lines of code of the original function. I've published it in the FileExchange not by posting the modified code, but as a program, which creates the copy of the m-file and inserts the new lines. Then only users, which own the required toolbox could use my modification and the copy right was not touched.
Maybe this is the most reliable way for you.
  2 Comments
Steven Lord
Steven Lord on 11 Nov 2021
@Jan, have you submitted an enhancement request asking for MathWorks to make that change to filtfilt permanently? Creating "patch files" like that could break if we refactor filtfilt in such a way that the replacement changes the wrong location in the file.
I'm not guaranteeing that we would make that change, but it could eliminate the potential need to modify your patching function to react to changes we may make in future releases.
Jan
Jan on 11 Nov 2021
Edited: Jan on 11 Nov 2021
@Steven Lord: Of course my patching function checked at first the version of the expected file. MathWorks has enhanced filtfilt one year later and I've removed this FEX submission. I've rewritten an equivalent tool from scratch, see: FEX: FilterM. Matlab's filtfilt has been improved many times in the last years. My version needs to be multi-threaded in the next step, but I do not find a fair estimation of the optimal number of threads.

Sign in to comment.

More Answers (0)

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!