how can we see the inner programming of any function??

30 views (last 30 days)
i am using the function "gradient". i want to see it's inner programming. can i see it.
thanks
Navneet nayan

Accepted Answer

John D'Errico
John D'Errico on 11 Apr 2017
Edited: John D'Errico on 11 Apr 2017
You often cannot see code. Much of MATLAB is provided as compiled code. This gives you efficient code, which of course you want very much. The MathWorks does NOT provide the source code for compiled tools, unless you work there.
Other tools that are written in m-files are viewable. NEVER use edit to examine code, as too often people mistakenly edit the code, then save it without realizing they did so. That will introduce random bugs into the code, a terribly bad thing to do because you will never expect to find an error there, so you will never look for it there. Therefore my recommendation that you never use edit to view code. (In fact, I saw this exact same problem happen quite recently. A user had a supplied function that appeared to be no longer working properly. The problem was they had mistakenly edited the function when looking at it, and then saved it.)
Instead, use the type command. For example, if you want to see how the nlinfit tool (from the stats toolbox) is written, then view it like this:
type nlinfit
If the function was provided in compiled form, then all that will be shown will be the block of help as returned by the help command.
  2 Comments
NAVNEET NAYAN
NAVNEET NAYAN on 11 Apr 2017
yeah, it's working but it opens in command window. how can i view it in editor...
Thanks
John D'Errico
John D'Errico on 11 Apr 2017
Edited: John D'Errico on 11 Apr 2017
You don't want to do that!!!!!! You CAN use edit, but that is a bad idea. I can tell you that you will be sorry, but people tend not to listen to advice, until they learn the hard way why it was good advice.
Mark Twain said this: "Good judgement is the result of experience and experience the result of bad judgement." It applies here. :)
You can scroll the command window as easily as you can scroll the editor.

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 11 Apr 2017
The gradient function is implemented as a MATLAB function file, so you can view it. If it's read-only in your installation (to prevent the possibility of modifying it) you can edit it. If it's writable, you may want to type or dbtype it instead to avoid accidental modification.
Note that not all functions in MathWorks products are implemented as MATLAB script, function, or class files. Particularly for the built-in functions, if you want to see their source code your first step would need to be here.
  3 Comments
Steven Lord
Steven Lord on 11 Apr 2017
I believe I remember that in the past we did offer that as an option in the Installer. I don't know off the top of my head if that's now the default (because of where MATLAB gets installed, it might be at least on Windows.) The installation documentation doesn't mention it, so it might be.
Jan
Jan on 12 Apr 2017
I install Matlab with admin privileges and work as standard user. Then overwriting built-in function is not possible.

Sign in to comment.

Categories

Find more on Introduction to Installation and Licensing in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!