Create a structure for statsfunhelper to record counters in manopt function S = statscounters(name) function S = statscounters(names) The input can either be one string containing a chosen name for a counter, or a cell containing multiple strings designating multiple counters. The names must be valid field names for Matlab structures. The output is a structure S. For each input string, S contains a field with that name. That field contains a function handle. Calling that function with appropriate inputs (problem, x, stats, store) returns the value of the counter saved in store and whose name is the field name. This manopt tool is meant to be used in conjunction with incrementcounter and with statsfunhelper. In the examples folder of the toolbox, the example named using_counters demonstrates how to use this feature. See also: statscounter statsfunhelper using_counters
0001 function S = statscounters(names) 0002 % Create a structure for statsfunhelper to record counters in manopt 0003 % 0004 % function S = statscounters(name) 0005 % function S = statscounters(names) 0006 % 0007 % The input can either be one string containing a chosen name for a 0008 % counter, or a cell containing multiple strings designating multiple 0009 % counters. The names must be valid field names for Matlab structures. 0010 % 0011 % The output is a structure S. For each input string, S contains a field 0012 % with that name. That field contains a function handle. Calling that 0013 % function with appropriate inputs (problem, x, stats, store) returns the 0014 % value of the counter saved in store and whose name is the field name. 0015 % 0016 % This manopt tool is meant to be used in conjunction with incrementcounter 0017 % and with statsfunhelper. In the examples folder of the toolbox, the 0018 % example named using_counters demonstrates how to use this feature. 0019 % 0020 % See also: statscounter statsfunhelper using_counters 0021 0022 % This file is part of Manopt: www.manopt.org. 0023 % Original author: Nicolas Boumal, July 27, 2018. 0024 % Contributors: 0025 % Change log: 0026 0027 % If we receive only one string as input, place it in a cell so that 0028 % the rest of this function's code works the same in both cases. 0029 if ischar(names) 0030 names = {names}; 0031 end 0032 0033 assert(iscell(names), ['names must be either one string, or a ' ... 0034 'cell of strings. Each string must be a ' ... 0035 'valid field name for structures.']); 0036 0037 for k = 1 : numel(names) 0038 0039 name = names{k}; 0040 0041 assert(isvarname(name) || iskeyword(name), ... 0042 'Each input string must be a valid structure field name.'); 0043 0044 S.(name) = @(problem, x, stats, store) ... 0045 getcountervalue(problem, x, stats, store, name); 0046 0047 end 0048 0049 end 0050 0051 function val = getcountervalue(problem, x, stats, store, name) %#ok<INUSL> 0052 if isfield(store.shared, name) 0053 val = store.shared.(name); 0054 else 0055 val = 0; 0056 end 0057 end