如何使用Matlab按字母顺序对属性-值对进行排序 [英] How to sort property -value pair in alphabetical order with Matlab
问题描述
我想向现有文件添加一个属性-值对.同时,所有属性应按字母顺序排序.例如:
I want to add a property-value pair to existing file. In the mean time all the properties should be ordered in alphabetical order. For example :
[Info] % property 1
value 1
[system] % property 2
value 2
如何添加其他属性,以便所有属性均按字母顺序排序.我能够使用将属性-值对添加到文件的末尾
fh = fopen(filename,'a')
,但我无法按字母顺序对它们进行排序.
How can i add additional property such that all properties will be sorted in alphabetical order. I was able to add property -value pair to the end of the file using
fh = fopen(filename,'a')
but i am not able to sort them alphabetically.
到目前为止,我尝试了以下操作,但是通过此操作,它仅打印新的property-value对.我想在打印新属性后再打印其余属性.
so far i tried this as follows but with this one it keeps printing only the new property-value pair . I want to print remaining properties onces it prints the new one.
function [] = myfun(filename ,propName,propvalue)
rfh = fopen(filename,'r');
tname = tempname();
wfh = fopen(tname,'w');
line = fgetl(rfh);
while ischar(line)
if (line(1) == '[') && (line(end) == ']')
property = lower(line(2:end-1)) % from ini file
String2 = property;
String1 = propName;
[sat] = sor(String1,String2)% subfunction
if sat == -1
fprintf(wfh,'[%s]\r\n%s\r\n',propName,propvalue);
else
fprintf(wfh,'%s\r\n',line);
end
else
fprintf(wfh,'%s\r\n',line);
end
line = fgetl(rfh);
end
fclose(rfh);
fclose(wfh);
movefile(tname,filename,'f')
function [sat] = sor(String1,String2)
Index = 1;
while Index < length(String1) && Index < length(String2) && String1(Index) == String2(Index)
Index = Index + 1;
end
% Return the appropriate code
if String1(Index) < String2(Index)
sat= -1
elseif String1(Index) > String2(Index)
sat= +1
else % the characters at this position are equal -- the shorter of the two strings should be "less than"
if length(String1) == length(String2)
sat = 0
elseif length(String1) < length(String2)
sat = -1
else
sat = +1
end
end
推荐答案
如何将文件读入struct
?
function fileData = readFileIntoStruct( fileName )
%
% read [property] value pairs file into struct
%
fh = fopen( fileName, 'r' ); % read handle
line = fgetl( fh );
while ischar( line )
% property
tkn = regexp( line, '\[([^\]+)]\]', 'once', 'tokens' );
% read next line for value
val = fgetl( fh );
fileDate.(tkn{1}) = val;
line = fgetl( fh ); % keep reading
end
fclose( fh ); % don't forget to close the file at the end.
现在,您拥有的所有数据都为struct
,其属性为fieldnames
,值作为field
值.
Now you have all the data as a struct
with properties as fieldnames
and values as the field
value.
现在您可以通过以下方式简单地更新属性:
Now you can update a property simply by:
function fileData = updateProperty( fileData, propName, newVal )
if isfield( fileData, propName )
fileData.(propName) = newVal;
else
warning( 'property %s does not exist - please add it first', propName );
end
您可以添加属性:
function fileData = addProperty( fileData, propName, newVal )
if ~isfield( fileData, propName )
fileData.(propName) = newVal;
else
warning ( 'property %s already exists, use update to change its value', propName );
end
您可以使用 orderfields
按字母顺序对属性进行排序:
You can sort the properties alphabetically using orderfields
:
fileData = orderfields( fileData );
您只需使用以下命令即可将struct
写回到文件中:
You can write the struct
back to file simply using:
function writeDataToFile( newFileName, fileData )
fopen( newFileName , 'w' ); %write handle
propNames = fieldnames( fileData );
for ii = 1:numel( propNames )
fprintf( fh, '[%s]\r\n%s\r\n', propNames{ii}, fileData.(propNames{ii}) );
end
fclose( fh );
假设:
-
属性名称是合法的Matlab字段名称(有关详细信息,请参见变量命名).
The properties' names are legitimate Matlab field names (see variable naming for details).
每个属性的值始终是一个字符串.
The value of each property is always a string.
在这些示例中,我没有包含任何错误检查代码(找不到文件,格式错误的字符串等)
I did not include any error-checking code in these examples (files not found, wrongly formatted strings, etc.)
我假设输入文件严格是"[prop] val"对,而没有任何其他注释等.
I assume the input file is strictly "[prop] val" pairs without any additional comments etc.
这篇关于如何使用Matlab按字母顺序对属性-值对进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!