Skip to content

Commit

Permalink
Account for varargin being a struct
Browse files Browse the repository at this point in the history
When called from places like optmagstr, varargin is actually a struct.
Also, the struct contains many other additional parameters to do with
fitting, so before calling genmagstr in optmagstr,
warning('off','sw_readparam:UnreadInput') is called. This stops
sw_readparam from issuing warnings. This also stops the
`spinw:genmagstr:UnreadInput` warning from being triggered, which
is why its name has been changed to UnreadInput, to avoid ugly
unecessary warnings.
  • Loading branch information
rebeccafair committed Aug 17, 2022
1 parent cf1060b commit f3c8aa2
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
8 changes: 4 additions & 4 deletions +sw_tests/+unit_tests/unittest_spinw_genmagstr.m
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
{'n', [0 1 1], 'mode', 'direct', 'S', [1; 0; 0]}, ...
{'mode', 'tile', 'k', [0 0 1/2], 'n', [0 0 1], 'S', [1; 0; 0]}, ...
{'mode', 'helical', 'k', [0 0 1/3], 'S', [1; 0; 0;], 'x0', []}, ...
{'mode', 'func', 'x0', [pi/2 -pi/4 0 0 [0 0 1/3] pi pi/2], 'unit', 'lu'}, ...
{'mode', 'fourier', 'S',[1; i; 0], 'n', [1 0 0]}
{'mode', 'func', 'x0', [pi/2 -pi/4 0 0 [0 0 1/3] pi pi/2], 'unit', 'lu', 'next', [1 2 1]}, ...
{'mode', 'fourier', 'S', [1; i; 0], 'n', [1 0 0]}
};
complex_n_input = {[1 1+i 0], [i 0 0]};
rotate_phi_inputs = {{'phi', pi/4}, {'phid', 45}, {'phi', pi/4, 'phid', 90}};
Expand Down Expand Up @@ -110,15 +110,15 @@ function test_tile_too_few_S_raises_error(testCase)
function test_ignored_input_warns(testCase, ignored_inputs)
testCase.verifyWarning(...
@() testCase.swobj.genmagstr(ignored_inputs{:}), ...
'spinw:genmagstr:UnusedInput')
'spinw:genmagstr:UnreadInput')
end
function test_rotate_ignored_input_warns(testCase)
swobj = copy(testCase.swobj);
% Need to initialise structure before rotating it
swobj.genmagstr('mode', 'direct', 'S', [1; 0; 0]);
testCase.verifyWarning(...
@() swobj.genmagstr('mode', 'rotate', 'k', [0 0 1/3]), ...
'spinw:genmagstr:UnusedInput')
'spinw:genmagstr:UnreadInput')
end
function test_helical_spin_size_incomm_with_nExt_warns(testCase)
swobj = copy(testCase.swobj);
Expand Down
20 changes: 13 additions & 7 deletions swfiles/@spinw/genmagstr.m
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,16 @@ function genmagstr(obj, varargin)
% Error if S or k is provided but is empty. This means it has failed the
% input validation, but hasn't caused an error because soft=True
err_str = [];
if any(strcmp(varargin, 'S')) && isempty(param.S)
if isstruct(varargin{1})
varargin_struct = varargin{1};
else
varargin_struct = struct(varargin{:});
end
varargin_names = fields(varargin_struct);
if any(strcmpi(varargin_names, 'S')) && isempty(param.S)
err_str = ["S"];
end
if any(strcmp(varargin, 'k')) && isempty(param.k)
if any(strcmpi(varargin_names, 'k')) && isempty(param.k)
err_str = [err_str "k"];
end
if length(err_str) > 0
Expand Down Expand Up @@ -286,13 +292,13 @@ function genmagstr(obj, varargin)
if ~any(strcmp(fields(mode_args), param.mode))
error('spinw:genmagstr:WrongMode','Wrong param.mode value!');
else
input_arg_names = varargin(1:2:end);
input_arg_names(ismember(input_arg_names, ["mode" "norm"])) = [];
unused_args = setdiff(input_arg_names, mode_args.(param.mode));
unused_args = {varargin_names{:}};
unused_args(ismember(lower(unused_args), ["mode" "norm"])) = [];
unused_args(ismember(lower(unused_args), lower(mode_args.(lower(param.mode))))) = [];
if ~isempty(unused_args)
warning('spinw:genmagstr:UnusedInput', ...
warning('spinw:genmagstr:UnreadInput', ...
'Input(s) %s will be ignored in %s mode', ...
join(unused_args, ', '), param.mode)
string(join(unused_args', ', ')), param.mode)
end
end

Expand Down

0 comments on commit f3c8aa2

Please sign in to comment.