matlab GOTO (sort of)

2019-09-17 06:46发布

问题:

I have a long script calling many other scripts that spit out a lot of figures.

For debugging purposes I think it would be useful if -when clicking on a picture- the editor goes to a specific line. Does anyone know if this is possible?

I thought I could implement something like

A=[];
figure
plot(x)

A=ginput(1)

if A~=[]
  goto(pointer,line)
end

The problem is that it only would work only right after that the figure is made. Not at the end of the analysis when I take a look to the figures for eventual errors.

In other words: is there a way to go to a specific line of the code by clicking on a figure?

N.B. For an implementation of GOTO see (GOTO FileExchange)

回答1:

Try using the opentoline function in conjunction with a callback. Something like

plot(1:10); % A simple plot
set(gcf,'ButtonDownFcn',@(h,e)opentoline('YourFunctionName.m',LineNumber));

In this case, when you click on the figure (not the axis, or any labels, but the grey part of the figure) the m file will open in the editor to the line specified.

If you want to programmatically determine the line number then do the following

plot(1:10)
st = dbstack;
set(gcf,'ButtonDownFcn',@(h,e)opentoline(st(1).file,st(1).line-1));


回答2:

You can use this undocumented function:

f = figure;
uicontrol ( 'string', 'open "str2double.m" @ 200', 'callback',  @(a,b)opentoline(which('str2double.m'),200), 'position', [100 100 400 50] )

It works for me in r2015a