MATLAB: Modifying built-in functions and debugging

breakpointbuilt-incommand linedebugmass-lumpingMATLABmodifyreaction-diffusionsolvestationarynonlinear

Hello,
for my project (stabilization of a reaction-diffusion problem with mass-lumping of a matrix) I would like to make a slight change in the actual version of the built-in MATLAB function solveStationaryNonlinear.m.
I'm almost sure it's not possible, since every change I try to introduce in the built-in function can't be saved.
So, in order to solve the problem now I need to:
1) run the code in debug mode;
2) stop the execution in solveStationaryNonlinear.m, when the matrix I want to modify has been created;
3) modify the matrix from the command line;
4) re-start running the code.
Since this matrix is modified at every step of a for loop, I have to stop the execution during each iteration, and this makes the code slow and difficult to use for any other user.
I can see two possible solutions, but I'm not able to get them right:
1) save modified MATLAB built-in functions (but I'm almost sure it's impossible, as I have already said);
2) write the debugging commands (such as dbstop, dbstep…) and the modification of the matrix corresponding to steps 1)-4) in a separate m-file, and then run this file instead of using the command line, so that any other user would just have to wait for the code to run, without writing on the command line. This seemed to be the solution to me; however, the execution just stops after introducing the breakpoint in solveStationaryNonlinear.m (as it should be, unfortunately), and the only way to modify the matrix is by operating from the command line.
Any help would be appreciated (even different ideas!).
Thanks,
Claudio

Best Answer

You can simply change a builtin function, if you obtain admin privilegs to get write permissions in Matlab's toolbox folders. But it is strongly recommened to avoid this.
But you can create a copy of the file and save it with another name in a uaser defined folder. Then you can apply any changes you want without disturbing the original function.
You can shadow the built-in function also: Save the function with the original name to a user-defined folder and add this folder on top of Matlab's PATH (see addpath or pathtool). Then Matlab uses the version, which is found at first in the PATH. But notice that shadowing built-in function is an evil source of bugs. If you e.g. change strcmp and insert a bug, you cannot open any files in the editor anymore, because Matlab uses this function for opening files.