0

对于.ini带有对的文件property_value,我想定义数据类型,property_name以便我将根据输入参数数据类型转换数据类型。

出于这个原因,我一直在尝试从 ini 文件中以 struct 格式读取指定的数据类型。但是我收到错误消息:invalid fieldname。如何定义property_name和数据类型以便我能够读取它,并且如果我想添加property_datatype_vlaue对也能够写入 ini 文件?

[system] % string
value
[application] % string
value

我还需要能够从我的输入参数中设置此数据类型。

如何使用 Matlab 按字母顺序对属性值对进行排序

4

1 回答 1

1

修改struct从此答案以包含值和类型

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.

假设:

  1. 属性的名称是合法的 Matlab 字段名称(有关详细信息,请参阅变量命名)。

  2. 每个属性的值始终是一个字符串。

  3. 我没有在这些示例中包含任何错误检查代码(未找到文件、格式​​错误的字符串等)

  4. 我假设输入文件是严格的“[prop] val”对,没有任何额外的注释等。

于 2013-01-24T13:41:48.030 回答