Modifying built-in functions and debugging

18 次查看(过去 30 天)
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

采纳的回答

Jan
Jan 2016-11-10
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.
  4 个评论
claudio
claudio 2016-11-10
Sorry for the long answer, but I'll try to be as clear as I can.
Before asking the question here, I tried to put in my folder the function solveStationaryNonlinear.m (with proper modifications) without specifying anything about paths.
But it didn't work; debugging the code, I noticed that (when calling the function above) MATLAB entered its version of the function, i.e. the unmodified toolbox version.
To be clear, I also tried to change the name of the function (something like MYsolveStationaryNonlinear.m), modifying the calls of the function in related files too.
A simple scheme of the situation is the following: in my code, I call the built-in function "solvepde", which calls (among many others) the built-in function "solveStationaryNonlinear".
So, I created functions "MYsolvepde" and "MYsolveStationaryNonlinear", and I adjusted the lines where these were called with the new names.
Executing, MATLAB shut down almost immediately with the error "Cannot access method 'performSolverPrecheck' in class 'pde.PDEModel'."; note that 'performSolverPrecheck' is the first command of MYsolvepde.m which recalls another function (third command overall).
So I tried to debug the code step by step, and here's the weirdest thing of all (in my opinion): instead of entering in 'performSolverPrecheck', MATLAB entered in a "classdef" (I don't know what this is) called "CoefficientAssignmentsRecords" and started to delete some parameters which I had defined, probably clearing some variables just before printing the error message.
So, MATLAB had understood that there was something strange with the code, but I can't figure out how that is possible, since I had changed the names!
After reading your suggestion, I came back to the original names (without "MY") and I typed in my folder "addpath('C:\Users\Claudio\...\Codes\', '-begin')", since the modified version of solveStationaryNonlinear.m was in the "Codes" folder.
Using "path" to view folder on path, this folder was indeed the first one, preceding all the other folders, including the folder in the toolbox which contains the built-in function.
Nevertheless, it didn't work as I had hoped: calling solveStationaryNonlinear.m, MATLAB entered in the built-in function instead of entering in my "modified" function, even if the name was the same and the priority seemed correct...
Thank you,
Claudio
claudio
claudio 2016-11-10
Dear Jan,
I don't know the exact reason, but now I can edit built-in functions from their original location... probably it has to do with the brave attempts I tried on my MATLAB folder.
I want to thank you for your suggestions, promising that I'll try to modify built-in functions as little as possible!
Claudio

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Get Started with MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by