问题
In the MatlabⓇ IDE, is there any easy way to jump to the definition of a particular method, without knowing in what class it is defined?
For self-contained functions, I can type edit funcname.m
. The same for classes. However, the hierarchy of superclasses for a particular class may be large. By using metaclasses, I can find out in what class a method was defined, then open the class, and browse to the appropiate definition. This is a lot more work than it is for self-contained functions.
From the interactive prompt, is there any direct way to either jump to the definition of a particular method, or put a breakpoint in this method (so that executing it will cause the editor to jump to the definition)?
回答1:
Actually in MATLAB's IDE you can move between functions within a file. If you want to get more information about a function that you are calling in your code, you can use Open Selection (CTRL+D with Windows keybindings) to quickly jump to where the function is defined.
Check this link: MATLAB Spoken Here
if you move the cursor on a function in the editor and then press ctrl+D the function will be open in the editor. for methods inside a class you can use 'Go To'. If the method is defined in the present class.
回答2:
You can use which
to locate a function when given specific input arguments.
Say for example we have the following files on the path:
>> which -all fun
C:\Users\Amro\Desktop\fun.m
C:\Users\Amro\Desktop\Klass.m % Klass method
Klass.m
classdef Klass < handle
methods
function fun(obj)
disp('hello from Klass')
end
end
end
fun.m
function fun()
disp('hello from fun')
end
Now we can differentiate between the two given what arguments they take:
>> o = Klass();
>> which('fun(o)')
C:\Users\Amro\Desktop\Klass.m % Klass method
>> which('fun')
C:\Users\Amro\Desktop\fun.m
Note that in the class method case, we have to use the syntax: fun(obj, args, ...)
as opposed to obj.fun(args, ...)
You can open the editor to the specified function using the matlab.desktop.editor
API:
matlab.desktop.editor.openAndGoToFunction(which('Klass'),'fun');
来源:https://stackoverflow.com/questions/18278093/jump-to-method-definition-regardless-of-class-it-is-defined-in