Modify built-in Matlab function without overwriting it
22 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
John D'Errico
el 10 de Nov. de 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.
Respuesta aceptada
Jan
el 11 de Nov. de 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 comentarios
Steven Lord
el 11 de Nov. de 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
el 11 de Nov. de 2021
Editada: Jan
el 11 de Nov. de 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.
Más respuestas (0)
Ver también
Categorías
Más información sobre Monte Carlo Analysis en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!