function Result = ini2struct(FileName)
%==========================================================================
% Version: 733341.4155741782200
%==========================================================================
%
% INI = ini2struct(FileName)
%
% This function parses INI file FileName and returns it as a structure with
% section names and keys as fields.
%
% Sections from INI file are returned as fields of INI structure.
% Each fiels (section of INI file) in turn is structure.
% It's fields are variables from the corresponding section of the INI file.
%
% If INI file contains "oprhan" variables at the beginning, they will be
% added as fields to INI structure.
%
% Lines starting with ';' and '#' are ignored (comments).
%
% See example below for more information.
%
% Usually, INI files allow to put spaces and numbers in section names
% without restrictions as long as section name is between '[' and ']'.
% It makes people crazy to convert them to valid Matlab variables.
% For this purpose Matlab provides GENVARNAME function, which does
% "Construct a valid MATLAB variable name from a given candidate".
% See 'help genvarname' for more information.
%
% The INI2STRUCT function uses the GENVARNAME to convert strange INI
% file string into valid Matlab field names.
%
% [ test.ini ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
% SectionlessVar1=Oops
% SectionlessVar2=I did it again ;o)
% [Application]
% Title = Cool program
% LastDir = c:\Far\Far\Away
% NumberOFSections = 2
% [1st section]
% param1 = val1
% Param 2 = Val 2
% [Section #2]
% param1 = val1
% Param 2 = Val 2
%
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
% The function converts this INI file it to the following structure:
%
% [ MatLab session (R2006b) ]~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
% >> INI = ini2struct('test.ini');
% >> disp(INI)
% sectionlessvar1: 'Oops'
% sectionlessvar2: 'I did it again ;o)'
% application: [1x1 struct]
% x1stSection: [1x1 struct]
% section0x232: [1x1 struct]
%
% >> disp(INI.application)
% title: 'Cool program'
% lastdir: 'c:\Far\Far\Away'
% numberofsections: '2'
%
% >> disp(INI.x1stSection)
% param1: 'val1'
% param2: 'Val 2'
%
% >> disp(INI.section0x232)
% param1: 'val1'
% param2: 'Val 2'
%
% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
%
% NOTE.
% WhatToDoWithMyVeryCoolSectionAndVariableNamesInIniFileMyVeryCoolProgramWrites?
% GENVARNAME also does the following:
% "Any string that exceeds NAMELENGTHMAX is truncated". (doc genvarname)
% Period.
%
% =========================================================================
Result = []; % we have to return something
CurrMainField = ''; % it will be used later
f = fopen(FileName,'r'); % open file
while ~feof(f) % and read until it ends
s = strtrim(fgetl(f)); % Remove any leading/trailing spaces
if isempty(s)
continue;
end;
if (s(1)==';') % ';' start comment lines
continue;
end;
if (s(1)=='#') % '#' start comment lines
continue;
end;
if ( s(1)=='[' ) && (s(end)==']' )
% We found section
CurrMainField = genvarname((s(2:end-1)));
Result.(CurrMainField) = []; % Create field in Result
else
% ??? This is not a section start
[par,val] = strtok(s, '=');
val = CleanValue(val);
if ~isempty(CurrMainField)
% But we found section before and have to fill it
Result.(CurrMainField).((genvarname(par))) = val;
else
% No sections found before. Orphan value
Result.((genvarname(par))) = val;
end
end
end
fclose(f);
return;
function res = CleanValue(s)
res = strtrim(s);
if strcmpi(res(1),'=')
res(1)=[];
end
res = strtrim(res);
return;
Matlab仿真实验室
- 粉丝: 4w+
- 资源: 2437
最新资源
- 【创新无忧】基于能量谷优化算法EVO优化广义神经网络GRNN实现数据回归预测附matlab代码.rar
- 【创新无忧】基于能量谷优化算法EVO优化相关向量机RVM实现数据多输入单输出回归预测附matlab代码.rar
- 【创新无忧】基于能量谷优化算法EVO优化极限学习机KELM实现故障诊断附matlab代码.rar
- 【创新无忧】基于能量谷优化算法EVO优化相关向量机RVM实现北半球光伏数据预测附matlab代码.rar
- 【创新无忧】基于黏菌优化算法LSMA优化广义神经网络GRNN实现电机故障诊断附matlab代码.rar
- 【创新无忧】基于黏菌优化算法LSMA优化广义神经网络GRNN实现数据回归预测附matlab代码.rar
- 【创新无忧】基于黏菌优化算法LSMA优化广义神经网络GRNN实现光伏预测附matlab代码.rar
- 【创新无忧】基于黏菌优化算法LSMA优化相关向量机RVM实现北半球光伏数据预测附matlab代码.rar
- 【创新无忧】基于黏菌优化算法LSMA优化极限学习机ELM实现乳腺肿瘤诊断附matlab代码.rar
- 【创新无忧】基于黏菌优化算法LSMA优化极限学习机KELM实现故障诊断附matlab代码.rar
- 【创新无忧】基于黏菌优化算法LSMA优化相关向量机RVM实现数据多输入单输出回归预测附matlab代码.rar
- 【创新无忧】基于牛顿拉夫逊优化算法NRBO优化广义神经网络GRNN实现电机故障诊断附matlab代码.rar
- 【创新无忧】基于牛顿拉夫逊优化算法NRBO优化广义神经网络GRNN实现光伏预测附matlab代码.rar
- 【创新无忧】基于牛顿拉夫逊优化算法NRBO优化极限学习机KELM实现故障诊断附matlab代码.rar
- 【创新无忧】基于牛顿拉夫逊优化算法NRBO优化极限学习机ELM实现乳腺肿瘤诊断附matlab代码.rar
- 【创新无忧】基于牛顿拉夫逊优化算法NRBO优化广义神经网络GRNN实现数据回归预测附matlab代码.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈