필터 지우기
필터 지우기

Debugging a sequence of anonymous functions

조회 수: 3 (최근 30일)
seth patterson
seth patterson 2022년 11월 25일
댓글: seth patterson 2022년 11월 28일
Suppose you were given this code that loads a sequence of images stored in mat files but the code doesn't run out of the box.
clear data;
data.vis.path = '/data/images_vis_v2/';
data.vis.meta = load(fullfile(data.vis.path,'meta_data.mat'));
data.vis.files = dir(fullfile(data.vis.path,'*_frame.mat'));
data.lwir.path = '/data/images_lwir_v2/';
data.lwir.meta = load(fullfile(data.lwir.path,'meta_data.mat'));
data.lwir.files = dir(fullfile(data.lwir.path,'*_frame.mat'));
for sensor = {'vis','lwir'}
data.(sensor{1}).times = [];
for k=1:numel(data.(sensor{1}).files)
tok = regexp(data.(sensor{1}).files(k).name,'(\d{7})_frame.mat','tokens');
if ~isempty(tok)
data.(sensor{1}).times = [data.(sensor{1}).times,str2double(tok{1}{1})];
end
end
data.(sensor{1}).times = sort(unique(data.(sensor{1}).times));
end
data.times = sort(unique([data.vis.times,data.lwir.times]));
data.filename = @(sensor,index) fullfile(data.(sensor).path,sprintf('%07d_frame.mat',data.times(index)));
data.loadvar = @(sensor,field,index) getfield(nthout(1, @() load(data.filename(sensor,index),field)),field);
data.image = @(sensor,index) cellout(data.loadvar(sensor,'O_ref',index));
data.vis.camera = data.vis.meta.cams_used{1};
data.lwir.camera = data.lwir.meta.cams_used{1};
nrml = @(x) single(x)/max(single(x(:)));
get_image = @(n) nrml(data.image('vis',n+1));
fig = figure(1); clf; colormap(fig,'gray');
frame_numbers = [0:10:50];
images = arrayfun(@(n) get_image(n), frame_numbers, 'UniformOutput', false);
poses = arrayfun(@(n) eye(4), frame_numbers(2:end)-1, 'UniformOutput', false);
Instead you get the following stack trace:
Error using load
Unable to read file '/data/images_lwir_v2/0170218_frame.mat'. No such file or directory.
Error in stub (line 23)
data.loadvar = @(sensor,field,index) getfield(nthout(1, @() load(data.filename(sensor,index),field)),field);
Error in nthout (line 7)
case 1, [value] = feval(fcn, varargin{:});
Error in stub (line 23)
data.loadvar = @(sensor,field,index) getfield(nthout(1, @() load(data.filename(sensor,index),field)),field);
Error in stub (line 24)
data.image = @(sensor,index) cellout(data.loadvar(sensor,'O_ref',index));
Error in stub (line 28)
get_image = @(n) nrml(data.image('lwir',n+1));
Error in stub (line 32)
images = arrayfun(@(n) get_image(n), frame_numbers, 'UniformOutput', false);
Error in stub (line 32)
images = arrayfun(@(n) get_image(n), frame_numbers, 'UniformOutput', false);
How do you debug anonymous functions? Is there a way to put break points or step in? What about a pipeline of anonymous functions?
  댓글 수: 9
Stephen23
Stephen23 2022년 11월 28일
@seth patterson: are you trying it on the code shown in your question? It worked for me on the lines I tried. If you are attempting some other code, please upload it.
Where exactly are you clicking? In the margin?
seth patterson
seth patterson 2022년 11월 28일
I was right clicking on the numbers. I just tried left clicking and it works. Thanks.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2022년 11월 25일
편집: Walter Roberson 2022년 11월 25일
"I have to use the anonymous functions as is. I was just wondering if there's a easy way to debug them"
No, there is not. You can right-click on the line number where you assign an anonymous function to a variable, and it might give you the option to put a breakpoint inside the function so defined, but it does not always give that option. In that case you have to track down the place where the function is invoked and put a breakpoint at the invocation, and then when the breakpoint is hit, you have to use the tools to step IN to the call.
When you are stopped at a breakpoint inside an anonymous function, you can use the usual tools to examine the workspace, but that workspace is often not going to be all that informative, possibly only showing you the named parameters. If you have configured for datatips in the editor, then you might be able to point to captured variables to examine them; otherwise you need to dbup until you get to a location that contains a handle to the anonymous function, and then you have to use functions to examine the Workspace of the anonymous function. Because functions returns a structure, you could try
functions(HANDLE).Workspace{1}
but chances are that you are going to want to assign the results into a variable in order to be able to examine the individual variables. Assigning the output of a function to a variable is fine in traditional MATLAB, but if you happen to be inside a function that was defined such that there is an end statement matching the function statement then the workspace is static and you cannot create new variables. In such a case you will need to use global to create a new variable to assign the results of the function call to, in order to be able to examine the captured variables.
If an anonymous function has captured variables, then there is no way provided by Mathworks to view the source for the anonymous function with the values of the captured variables substituted in for the names of the captured variables. For example,
w = rand();
F = @(t) sin(2*pi*w*t)
then there is no way to ask MATLAB to display sin(2*pi*0.3479351*t) [or as appropriate].
  댓글 수: 1
seth patterson
seth patterson 2022년 11월 26일
Bummer, maybe math works could make some improvements in the future.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by