Property_value + matlab

前端 未结 1 1456
生来不讨喜
生来不讨喜 2021-01-28 03:17

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

1条回答
  •  梦毁少年i
    2021-01-28 03:46

    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:

    1. The properties' names are legitimate Matlab field names (see variable naming for details).

    2. The value of each property is always a string.

    3. I did not include any error-checking code in these examples (files not found, wrongly formatted strings, etc.)

    4. I assume the input file is strictly "[prop] val" pairs without any additional comments etc.

    0 讨论(0)
提交回复
热议问题