printf - Ideas / best practices for controlling verbosity in matlab functions -
let's have some_fun complicated work.
function foo = some_complicated_fun(x,y,z,verbosity)
i've written bit of debugging prints code, avg user may not interested in seeing if working. error statements (not enough args, etc) print regardless.
my current method writing function is:
function foo = some_complicated_fun(x,y,z,verbosity) (x,y,z,verbosity) = parse_args(x,y,z,verbosity); % parse args, check nargin, etc i=1:5, % whatever % work x,y,z % blah blah if verbosity fprintf('now on iteration %i\n',i); % basic print end % or, mutiple levels: if verbosity == 1; fprintf('now on iteration %i\n',i); % basic print end if verbosity == 2; fprintf('x = %f,y = %f,z=%f %i\n',x,y,z); % more information end % more work end
i don't doing if statements because clutters code, can't think of other way. 1 liner better , worse:
if verbosity; fprintf('iteration %i\n',i); end
better because it's smaller, worse because one-liners less readable same thing on 3 lines.
what want simple, more elegant way control verbosity.
does exist?
i have created method control log output verbosity. not finished yet, since question appeared uploaded to:
this link @ matlab file exchange.
it works setting global output log desired level, , show message on following format:
output_level: function_which_called_message your_message.
however, output messages higher output level set. default display info messages or higher, won't show debug , verbose messages. if want see debug messages, set:
output.level(output.disp_debug)
the output methods are:
output.verbose output.debug output.info output.warning output.error
you select messages displayed in functions doing output.'desired_level'('message',input_1,input2)
in sprintf
or fprintf
matlab format. i.e, output.info('the variable value %d',variable)
. warning , error messages may specify identifier, 'package:method:error_id', , doing so, output on matlab's warning
, error
functions. i.e output.warning('my_package:my_method:my_id','something wrong happened here!')
.
the output levels available are:
output.level(output.disp_verbose) % show messages output.level(output.disp_debug) % show debug info warning errors output.level(output.disp_info) % show info warning errors output.level(output.disp_warning) % show warning errors output.level(output.disp_mute) % show errors.
you can specify output log file using
output.place('file_path')
and replace log file (you can use reset current log):
output.place('file_path',true)
or reset screen
output.place(1)
here sample of output using output level disp_verbose:
>> nilmcontainer = nilmfilecontainer.newrun('inputfolder','sim_real/','runname','realhouse','samplesperfile',60*60*60); verbose: getfilesonfolder found 2 files on folder 'data/sim_real/'. following: verbose: getfilesonfolder [1]:data/sim_real/240am000.csv verbose: getfilesonfolder [2]:data/sim_real/240am001.csv debug: nilmfilecontainer.setrunname updating run name realhouse_run5. info: newrun reading file data/sim_real/240am000.csv. verbose: csv_data reading samples (ignored:0,read:216000,remaining:2422014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_1-19:12:46-30_10_2012.mat' verbose: csv_data reading samples (ignored:216000,read:216000,remaining:2206014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_2-20:12:45-30_10_2012.mat' verbose: csv_data reading samples (ignored:432000,read:216000,remaining:1990014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_3-21:12:45-30_10_2012.mat' verbose: csv_data reading samples (ignored:648000,read:216000,remaining:1774014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_4-22:12:45-30_10_2012.mat' verbose: csv_data reading samples (ignored:864000,read:216000,remaining:1558014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_5-23:12:45-30_10_2012.mat' verbose: csv_data reading samples (ignored:1080000,read:216000,remaining:1342014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_6-00:12:44-31_10_2012.mat' verbose: csv_data reading samples (ignored:1296000,read:216000,remaining:1126014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_7-01:12:43-31_10_2012.mat' verbose: csv_data reading samples (ignored:1512000,read:216000,remaining:910014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_8-02:12:43-31_10_2012.mat' verbose: csv_data reading samples (ignored:1728000,read:216000,remaining:694014) @ file: sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_9-03:12:43-31_10_2012.mat' verbose: csv_data reading samples (ignored:1944000,read:216000,remaining:478014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'nilm_data/realhouse_run5/realhouse_run5-file_10-04:12:42-31_10_2012.mat' verbose: csv_data reading samples (ignored:2160000,read:216000,remaining:262014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_11-05:12:41-31_10_2012.mat' verbose: csv_data reading samples (ignored:2376000,read:216000,remaining:46014) @ file: data/sim_real/240am000.csv. debug: addfiles writing file 'data/nilm_data/realhouse_run5/realhouse_run5-file_12-06:12:41-31_10_2012.mat' verbose: csv_data reading samples (ignored:2592000,read:46014,remaining:0) @ file: data/sim_real/240am000.csv. verbose: csv_data reading samples (ignored:0,read:169986,remaining:2799058) @ file: data/sim_real/240am001.csv. info: readfile found discontinuity while changing file 'data/sim_real/240am000.csv' 'data/sim_real/240am001.csv'.
as said, not @ final version, can me improve it! x)
Comments
Post a Comment