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;
![avatar](https://profile-avatar.csdnimg.cn/4f53cad2241c45de9c6c65be36411126_m0_62143653.jpg!1)
Matlab仿真实验室
- 粉丝: 4w+
- 资源: 2475
最新资源
- YOLOv11跨域迁移学习-遥感影像中违建检测的小样本训练方案.pdf
- YOLOv11模型轻量化-移动端实时垃圾分类与环保监管应用.pdf
- YOLOv11模型压缩技巧-移动端APP直播流中的实时弹幕遮挡检测.pdf
- YOLOv11模型压缩实战-嵌入式设备实时推理性能优化指南.pdf
- YOLOv11模型压缩实战-FPGA硬件加速下的实时视频流处理.pdf
- YOLOv11模型压缩与移动端部署-Android平台实时目标检测实战.pdf
- YOLOv11模型压缩术-剪枝量化一条龙推理速度提升5倍实战.pdf
- YOLOv11模型蒸馏实战-工业摄像头模组的低功耗部署方案.pdf
- YOLOv11实战教学-基于PyTorch的实时视频分析系统开发.pdf
- YOLOv11模型蒸馏与量化-工业级轻量化目标检测实战.pdf
- YOLOv11与SlowFast算法结合-视频行为识别实战开发指南.pdf
- YOLOv11在安防领域的创新应用-夜间红外目标检测算法调优.pdf
- YOLOv11在卫星遥感中的应用-地质灾害区域自动识别与评估.pdf
- YOLOv11在无人机巡检中的应用-电力设备缺陷识别与定位实践.pdf
- YOLOv11在遥感影像中的建筑提取与变化检测技术解析.pdf
- YOLOv11在制造业中的实时缺陷检测与MES系统集成方案.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback](https://img-home.csdnimg.cn/images/20220527035711.png)
![feedback-tip](https://img-home.csdnimg.cn/images/20220527035111.png)