Loads Preferences File

by

 

This function loads a .mat file containing a structure and appends all the loaded fields and respect

Load_Pref_File(prefs_default, prefs_file)
%+------------------------------------------------------------------------+
%+                          Load_Pref_File                                +
%+------------------------------------------------------------------------+
%+                       |Joao Tiago Fonseca  |                           +
%+------------------------------------------------------------------------+
%+ FUNCTION: Load_Pref_File                                               +
%+ DESCRIPTION: This function loads a mat file containing a structure and +
%+             appends all the loaded fields and respective values to an  +
%+             older structure.                                           +
%+              Any new fields are simply added.                          +
%+                                                                        +
%+              This is very usefull when working with preferences files, +
%+             where you may create a structure with the default values   +
%+             and then load the user preferences from previously saved   +
%+             file.                                                      +
%+              If for instance, the user file is older and doesn't       +
%+             contain newer fields, the default ones with fill the spots.+
%+                                                                        +
%+                                                                        +
%+  PREFS_NEW = LOAD_PREF_FILE(PREFS_DEFAULT, PREFS_FILE) , PREFS_DEFAULT +
%+             and PREFS_NEW are structure type. PREFS_FILE is a string   +
%+             containing the file name to be loaded.                     +
%+                                                                        +
%+------------------------------------------------------------------------+
%%

function prefs_new = Load_Pref_File(prefs_default, prefs_file)

	if ~isstruct(prefs_default)
		error('MATLAB:Load_Pref_File:InconsistentDataType', 'argument must be structure type');
	end

	prefs_new = prefs_default;
	prefs_user = load(prefs_file, '-mat');	% carrega a nova estrutura

	s_fields = fieldnames(prefs_user);		% nomes dos campos da estrutura carregada

	for i=1:size(s_fields, 1)
		prefs_new.(char(s_fields(i))) = prefs_user.(char(s_fields(i)));
	end

	prefs_new = orderfields(prefs_new);

Contact us