For an .ini
file with property_value
pair, I\'d like to define the datatype for property_name
such that I will convert the datatype based
Modifying the struct
from this answer to contain value and type
function fileData = readFileIntoStruct( fileName )
%
% read [property] value pairs file into struct
%
fh = fopen( fileName, 'r' ); % read handle
line = fgetl( fh );
while ischar( line )
% property
tkn = regexp( line, '\[([^\]+)]\]\s*%\s*([^%]+)\s*$', 'once', 'tokens' );
% read next line for value
val = fgetl( fh );
fileDate.(tkn{1}).val = val;
fileDate.(tkn{1}).type = tkn{2};
line = fgetl( fh ); % keep reading
end
fclose( fh ); % don't forget to close the file at the end.
Assumptions:
The properties' names are legitimate Matlab field names (see variable naming for details).
The value of each property is always a string.
I did not include any error-checking code in these examples (files not found, wrongly formatted strings, etc.)
I assume the input file is strictly "[prop] val" pairs without any additional comments etc.