Ini file read and write + add new property + matlab -
this question has answer here:
- property_value + matlab 1 answer
how read , write ini files: want add new property existing one. want write ini file temp file , add new property it. new property have head,name,desc, value , layout (like: line 1 & 3)
#---------------
# head
# --------------
[name]% type
# desc
value
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),'#') && strcmpi(line(3),'=') layout = line(2:end); elseif strcmpi(line(1),'#')&& ~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 define filedname (prop) 'layout' , 'header' found in lines before actual'prop'. % command window % undefined variable prop. % error in ecco2511>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 exists, use set change value',propname ); end filedata = orderfields( filedata ); propnames = fieldnames( filedata ); 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');
sounds need ini config matlab file exchange. allow read, write, , modify ini style files. have used number of times , works quite well.
Comments
Post a Comment