Mercurial > repos > public > sbplib
changeset 892:b7ec26da3d77
Handle default arguments better in dealStruct()
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Thu, 22 Nov 2018 07:26:45 +0100 |
parents | fd85412c7a99 |
children | 7ea4b6de59d9 |
files | dealStruct.m |
diffstat | 1 files changed, 14 insertions(+), 5 deletions(-) [+] |
line wrap: on
line diff
diff -r fd85412c7a99 -r b7ec26da3d77 dealStruct.m --- a/dealStruct.m Thu Nov 22 07:25:52 2018 +0100 +++ b/dealStruct.m Thu Nov 22 07:26:45 2018 +0100 @@ -1,9 +1,18 @@ function varargout = dealStruct(s, fields) - default_arg('fields', fieldnames(s)); + default_arg('fields', []); - assert(nargout == length(fields), 'Number of output arguements must match the number of fields'); - - for i = 1:length(fields) - varargout{i} = s.(fields{i}); + if isempty(fields) + out = dealFields(s, fieldnames(s)); + varargout = out(1:nargout); + else + assert(nargout == length(fields), 'Number of output arguements must match the number of fieldnames provided'); + varargout = dealFields(s, fields); end end + +function out = dealFields(s, fields) + out = cell(1, length(fields)); + for i = 1:length(fields) + out{i} = s.(fields{i}); + end +end