ctrl+d does not work for methods

12 次查看(过去 30 天)
Paul Mellor
Paul Mellor 2014-7-24
编辑: Drew Chap 2023-4-12
I can use ctrl+d to quickly open a function in order to browse the source code for it. However this does not seem to work for methods of an object. Since I use it a lot, this is seriously discouraging me from using OO in Matlab. Is there a way to make this work, or some other way to do it (needs to be quick and convenient)?

回答(3 个)

Roi
Roi 2021-10-14
Any update on this matter of openning files using ctrl+d on methods of an object ?
It is possible on other languages IDEs, why cant it work on also in Matlab?
This missing feature make debuging and wokring on less familiar code that has an object oriented structure very hard, especially on big projects. I find my self going back and forth to find the object construction, opening the files that defines the class and seaching for the correct method manualy.
I think that in the end of 2021 its about time a computer will do the work for me ;)
Hoping for some reply...
  1 个评论
Drew Chap
Drew Chap 2023-4-10
For me it works to double click the name of the method so that it is highlighted and then use CTRL+D to open it. I had no idea about this workaround until I submitted a bug report on it and that was the reply, but it works for me and is good enough for now.

请先登录,再进行评论。


Qiang Fu
Qiang Fu 2019-10-29
alse wanna know

Steven Lord
Steven Lord 2023-4-10
The problem is simple to state but quite a bit more complicated than you might expect. If you were to highlight the method name plot in a file and press Ctrl-D, which version of that function should it open? There are:
d = which('-all', 'plot')
d = 35×1 cell array
{'built-in (/MATLAB/toolbox/matlab/graph2d/plot)' } {'/MATLAB/toolbox/matlab/graphics/math/@digraph/plot.m' } {'/MATLAB/toolbox/matlab/graphics/math/@graph/plot.m' } {'/MATLAB/toolbox/matlab/timeseries/@timeseries/plot.m' } {'/MATLAB/toolbox/matlab/graphics/math/@polyshape/plot.m' } {'/MATLAB/toolbox/matlab/graphics/math/@alphaShape/plot.m' } {'/MATLAB/toolbox/matlab/bigdata/@tall/plot.m' } {'/MATLAB/toolbox/bioinfo/bioinfo/@phytree/plot.m' } {'/MATLAB/toolbox/bioinfo/microarray/@HeatMap/plot.m' } {'/MATLAB/toolbox/bioinfo/microarray/@clustergram/plot.m' } {'/MATLAB/toolbox/curvefit/curvefit/@cfit/plot.m' } {'/MATLAB/toolbox/curvefit/curvefit/@sfit/plot.m' } {'/MATLAB/toolbox/econ/econ/@conjugateblm/plot.m' } {'/MATLAB/toolbox/econ/econ/@diffuseblm/plot.m' } {'/MATLAB/toolbox/econ/econ/@mixconjugateblm/plot.m' } {'/MATLAB/toolbox/econ/econ/@lassoblm/plot.m' } {'/MATLAB/toolbox/econ/econ/@mixsemiconjugateblm/plot.m' } {'/MATLAB/toolbox/econ/econ/@empiricalblm/plot.m' } {'/MATLAB/toolbox/econ/econ/@blm/plot.m' } {'/MATLAB/toolbox/econ/econ/@customblm/plot.m' } {'/MATLAB/toolbox/econ/econ/@semiconjugateblm/plot.m' } {'/MATLAB/toolbox/ident/ident/@iddata/plot.m' } {'/MATLAB/toolbox/ident/nlident/@idnlarx/plot.m' } {'/MATLAB/toolbox/ident/nlident/@idnlhw/plot.m' } {'/MATLAB/toolbox/mpc/mpc/@mpc/plot.m' } {'/MATLAB/toolbox/robust/rctobsolete/robust/@frd/plot.m' } {'/MATLAB/toolbox/robust/robust/@umargin/plot.m' } {'/MATLAB/toolbox/shared/channel/rfprop/@propagationData/plot.m' } {'/MATLAB/toolbox/shared/drivingscenario/@drivingScenario/plot.m'} {'/MATLAB/toolbox/signal/signal/@dspdata/plot.m' }
35 different functions and/or methods by that name just in MathWorks toolboxes accessible on MATLAB Online. It is not always going to be possible to determine just through static analysis of the code around that call which of those functions Ctrl-D should open.
You could, if you had a variable in the workspace and were willing to modify your workflow a little, take advantage of a fairly little-used feature of the which function. If you specify an expression that calls a method, like the plot method on a graph object as the example below does, you can limit which methods are returned.
g = graph(bucky);
w = which('-all', 'plot(g)')
w = 2×1 cell array
{'/MATLAB/toolbox/matlab/graphics/math/@graph/plot.m'} {'built-in (/MATLAB/toolbox/matlab/graph2d/plot)' }
This only returned two of the 35 plot functions and/or methods. Without the '-all' option only the first of those would have been returned, as per the following example.
dbtype('1:5', which('plot(g)')) % Displays the @graph/plot.m
1 function p = plot(varargin) 2 % PLOT Plots an undirected graph 3 % 4 % PLOT(G) plots the undirected graph G. 5 %
You could copy that method call to the clipboard and write a shortcut or favorite that uses clipboard('paste') into a which call to determine the correct method to open then opens it with edit.
  1 个评论
Drew Chap
Drew Chap 2023-4-12
编辑:Drew Chap 2023-4-12
I think this is beside the point. The original post, and what many of us have struggled with, is that the behavior of CTRL+D is different when using it on a method called by an object, vs when using it on a plain function. For example:
myObject.some_method_with_a_very_unique_name()
Placing your cursor on the method name and performing CTRL+D should bring you to the definition of that method, as it does with any plain function, but instead it asks you if you want to create a file called "myObject.some_method_with_a_very_unique_name" which I can't imagine is ever the user's intention. But if you double-click on the method name and then perform CTRL+D then it brings you to the function definition as expected. I have not seen a reason why CTRL+D should behave differently for a class method based on whether the cursor is simply on the function name vs whether the entire function is highlighted.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by