Modifying built-in functions and debugging

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일

1 개 추천

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일
Dear Jan.
thank you for the answer. I tried your suggestions, and here's what happened:
1) I didn't know I could obtain admin privileges to save modified built-in functions. I tried to select my R2016b folder and change some security authorization, but I'm not expert enough to know what I'm doing exactly, so it didn't work;
2) I had already tried to copy the toolbox function I needed to my folder and save it with the same name, but MATLAB continued to see its built-in function instead of my modified copy. I tried with some debugging, but something strange happened, since MATLAB realized I was trying to "cheat" and shut the execution immediately (MATLAB realized that the path of the function wasn't that expected, instead of simply watching the name of the function, I think). I don't know if it happens because I'm using a toolbox, or because the call of this function implies the call of a lot of other built-in functions (not less than a hundred).
3) I tried the command addpath, and MATLAB added my folder on the top of the path actually... but nothing changed from 2).
It's true that my code works anyway using the command line, but if there is a simple solution to my problem I would like to know it!
Thanks,
Claudio
Jan
Jan 2016년 11월 10일
@Claudio: As I've mentioned, modifying Matlab's toolbox functions is a bad idea. But if you really have a good reason, you should not modify the permissions of Matlab's folders. This would be a security gap. I've said, that you should obtain admin privileges, not remove the restriction to admins for the folder. This is an important difference. If you are not sure, do not go this way. You might damage your Matlab installation seriously.
2. It is not enough to copy the function to any folder, but it must be a folder, which appears on top of Matlab's PATH. Matlab uses the first file, which is found in the PATH. So if you add anything at the end, it is shadowed. (There is an execption with the current folder, but I recommend to to rely on this volatile detail.)
Please explain, what "shut the execution" exactly mean. Do you get an error message? Why do you assume, that Matlab thinks, that you are cheating? You cannot know, what "Matlab realizes".
Perhaps the function does not call only other toolbox functions, but also some functions in the /private subfolder. Then creating a copy in a folder on top of the PATH is not working.
3) Please explain, what you have done exactly: What is found in the folder, which is added to the path? What happens?
I've patched a lot of Matlab function with faster versions by a user-defined folder on top of the path. This works in general.
I've heared of checks for copied toolbox functions, but this should not happen, if you rename the function. Did you try this?
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개)

카테고리

도움말 센터File Exchange에서 Get Started with MATLAB에 대해 자세히 알아보기

제품

질문:

2016년 11월 9일

댓글:

2016년 11월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by