5.0

5.0 | 1 rating Rate this file 4 Downloads (last 30 days) File Size: 2.45 KB File ID: #17177

ini2struct

by Andriy Nych

 

26 Oct 2007 (Updated 26 Oct 2007)

Function parses INI file and returns it as a structure with section names and keys as fields

| Watch this File

File Information
Description

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)

Acknowledgements
This submission has inspired the following:
struct2ini
MATLAB release MATLAB 7.3 (R2006b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (1)
04 Aug 2008 Aleks V

Nice tool, I also added support for numeric types by adding two lines:

[val_cand status] = str2num(val);
if status, val = val_cand; end;

Thanks!

Please login to add a comment or rating.
Tag Activity for this File
Tag Applied By Date/Time
data import Andriy Nych 22 Oct 2008 09:32:57
data export Andriy Nych 22 Oct 2008 09:32:57
structure Andriy Nych 22 Oct 2008 09:32:57
ini file Andriy Nych 22 Oct 2008 09:32:57
data export Dirk Lohse 13 Nov 2008 05:13:11

Contact us at files@mathworks.com