title: Ini file read and write + add new property + matlab
body: <p>How to read and write ini files: I want to add new property to an existing one. I want to write the ini file to temp file and finally add the new property to it. the new property will have head,name,desc, value and layout (like: line 1 &amp; 3)</p>
  
  <p>#--------------- <br/>
   # head <br/>
   # --------------<br/>
   [name]% type <br/>
   # desc <br/>
   value</p>
  
  <pre><code>fileData = [];
  fh = fopen( fileName, 'r' ); % read handle
  tname=tempname();
  wfh =fopen(tname,'w'); % write handle
  line = fgetl(fh);
  val = '';
  Prop ='';
  type = '';
  header = '';
  desc= '';
  
  while ischar(line)
  
      if strcmpi(line(1),'#') &amp;&amp; strcmpi(line(3),'=')
          layout = line(2:end);
      elseif strcmpi(line(1),'#')&amp;&amp; ~strcmpi(line(3),'=')
          header = line(2:end);
      else 
          Prop = regexp(line,{'\[*\w+\]\s*%\s*.*\s*'},'match');
      [property data_type] = strtok(Prop,'%')
      prop_p = property{1};% property_name
      prop_per  = regexprep(prop_p,{'\[','\]'},'');
      prop = prop_per{1};
      Dtype = data_type{1}; % datatype
      dtype = strtrim(strrep(Dtype,'%',''));
      DATA_type = dtype{1};
  
  end 
  
  
  
   % How can i define filedname (prop) for the 'layout' and 'header' as they are found in lines before the actual'prop'.
  
    % From command window
  
    % Undefined variable prop.
  
    % Error in ecco2511&gt;add (line 212)
    % fileData.(prop{1}).layout = layout;
  
  
      desc = fgetl(fh); % description
      desc = desc(1:end);
      line = fgetl(fh);
      val = line(1:end);
  
      fileData.(prop).layout = layout;
      fileData.(prop).header = header;
      fileData.(prop).type = DATA_type;
      fileData.(prop).desc = desc;
      fileData.(prop).val = val;
  
      line = fgetl(fh); % keep reading
  
  end
  
  if ~isfield( fileData, propName)
      fileData.(propName).val = newVal;
      fileData.(propName).type = datatype;
      fileData.(propName).desc = description;
      fileData.(propName).layout = layout;
      fileData.(propName).header = header;
  
  else
      error ( 'property %s already exists, use set to change its value',propName );
  end
  
  fileData = orderfields( fileData );
  propNames = fieldnames( fileData );
  
  for ii = 1:numel( propNames )
  
      fprintf(wfh,'%s\r',fileData.(propNames{ii}).layout);
      fprintf(wfh,'\n');
      fprintf(wfh,'%s\r',fileData.(propNames{ii}).header);
      fprintf(wfh,'\n');
      fprintf(wfh,'%s\r',fileData.(propNames{ii}).layout);
      fprintf(wfh,'\n');
      fprintf( wfh, '[%s]%s\r', (propNames{ii}),fileData.(propNames{ii}).type);
      fprintf(wfh,'\n');
      fprintf( wfh,'#%s\r',fileData.(propNames{ii}).desc);
      fprintf(wfh,'\n');
      fprintf( wfh,'%s\r',fileData.(propNames{ii}).val);
      fprintf(wfh,'\n');
  end
  fclose(fh);
  fclose(wfh);
  [status,errmsg]= movefile(tname,fileName,'f');
  </code></pre>