rec_fresnel: Not enough input arguments.
Summary
While reconstructing the Astronaut hologram, the error Not enough input arguments
is raised.
Steps to reproduce
- Download the Astronaut hologram
- Place the hologram on the root dir of the project
- Run the following script:
load(hologram_path)
info = getSettings('cfg_file', config_path, ...
'apertureinpxmode', true, ...
'ap_sizes', [1940 2588], ...
'h_pos', 0, ...
'v_pos', 0);
nrsh(double(u1), -0.172, info)
Current behavior
The script exits with the following error stack:
Not enough input arguments.
Error in rec_fresnel (line 135)
if (isLast), clear F; end
Error in num_rec>@(dh,p,wlen,z,pad,dir,isLast)rec_fresnel(dh,p,wlen,z,pad,dir,isLast) (line 36)
fun = @(dh, p, wlen, z, pad, dir, isLast)rec_fresnel(dh, p, wlen, z, pad, dir, isLast);
Error in num_rec (line 40)
hol_rendered(:, :, idx) = fun(hol_rendered(:, :, idx), ...
Error in nrsh (line 497)
[hol_rendered] = num_rec(hol_rendered, info, ...
Error in Astronaut (line 12)
nrsh(double(u1), -0.172, info)
The function rec_fresnel
is being called with only 7 arguments, with the ref_wave_rad
argument missing:
hol_rendered(:, :, idx) = fun(hol_rendered(:, :, idx), ...
info.pixel_pitch, ...
info.wlen(idx), -rec_dist, ...
info.zero_pad, info.direction, isLast);
While the function documentation states that ref_wave_rad
is optional, this is not the case. In the initialization section of the rec_fresnel
script, if less than 7 arguments are passed, ref_wave_rad
is set to 0.
%% 0) Initialization
% Initialize missing arguments
if nargin < 7
ref_wave_rad = 0;
if nargin < 6
direction = 'forward';
if nargin < 5
zero_pad = false;
if nargin < 4
error('nrsh:rec_fresnel:input_args', 'Error in rec_fresnel: not enough input arguments.')
end
end
end
end
However, this type of initialization should be done carefully. In the case of this bug, the ref_wave_rad is missing
, therefore, the value of isLast
will be attributed to ref_wave_rad
, which is then set to 0 because nargin
= 7. Unlike most popular programming languages, MATLAB can't automatically distinguish which of the arguments is missing in the function call. If only zero_pad
was missing, for example, ref_wave_rad
would be set to 0 regardless, and zero_pad
would not be set to false.
Fix
There are several things to be done to fix this:
- Starting on the
rec_fresnel
function, thenargin
requirements should be updated as follows to account for the newisLast
argument:
if nargin < 8
ref_wave_rad = 0;
if nargin < 7
direction = 'forward';
if nargin < 6
zero_pad = false;
if nargin < 5
error('nrsh:rec_fresnel:input_args', 'Error in rec_fresnel: not enough input arguments.')
end
end
end
end
- In the
num_rec
function, add theref_wave_rad
argument to the function handle within thefresnel
case:
fun = @(dh, p, wlen, z, pad, dir, ref_wave_rad, isLast)rec_fresnel(dh, p, wlen, z, pad, dir, ref_wave_rad, isLast);
- Add the default rad_wave_rad to the function handle call:
hol_rendered(:, :, idx) = fun(hol_rendered(:, :, idx), ...
info.pixel_pitch, ...
info.wlen(idx), -rec_dist, ...
info.zero_pad, info.direction, 0, isLast);
- To avoid having to pass a
ref_wave_rad
value, on therec_fresnel
function header I advise placing the optional arguments at the end of the function call:
rec_fresnel(hol, pitch, wlen, rec_dist, zero_pad, direction, isLast, ref_wave_rad)
Therefore, in the case that the ref_wave_rad
is not passed as an argument, the value of isLast
won't be assigned to ref_wave_rad
, and will be correctly assigned as 0 in the initialization section.
Relevant logs
System information
Software information
- NRSH version: 12.0
- MATLAB version: 9.4.0.813654 (R2018a)
-
Relevant enabled toolboxes:
- Image Processing Toolbox Version 10.2 (R2018a)
- Signal Processing Toolbox Version 8.0 (R2018a)
- Statistics and Machine Learning Toolbox Version 11.3 (R2018a)
- OS: Manjaro Linux x86_64
- Kernel: 6.3.12-1-MANJARO
Hardware information
- CPU: AMD Ryzen 5 5600G with Radeon Graphics (12) @ 3.900GHz
- GPU: AMD ATI Radeon Vega Series / Radeon Vega Mobile Series
- Memory: 16 GB