matlab - 如何将属性 _value 对添加到 txt 文件并按字母顺序对它们进行排序

标签 matlab

我想在包含属性值对的 txt 文件中添加属性值对,属性应按字母顺序排序,其中属性位于方括号之间,其值在下一行中。这是一个示例文件:除此之外,我想忽略以“#”开头的注释行。

#
[system]
# 
programming 
#
[information] 
#
application

喜欢:-

function [] = updateFile( fileName,property,propertyValue )

% all inputs in strings
%
rfh = fopen( fileName, 'r' ); % read handle
tname = tempname(); % temporary file name
wfh = fopen( tname, 'w' )

在这个例子中,“系统”是一个属性,“编程”是它的值。同样,“信息”是另一个属性,“应用程序”是它的值。

我想用属性值对调用我的函数,并用新的属性值对更新 txt 文件。

最佳答案

由于您正在更新文件,因此您应该以“追加”模式打开它。您使用 sort 函数对数据进行排序。假设变量 propertypropertyValue 是元胞数组,您的代码将如下所示

function [] = updateFile( fileName,property,propertyValue )

% all inputs in strings

fid = fopen(fileName, 'a' ); % file handle
[property_sorted,sort_index] = sort(property); % sort file
for count = 1:length(sort_index)
    fprintf(fid,'%s\n%s\n',property_sorted(count),propertyValue(sort_index(count)));
end

fclose(fid);

有关更多信息,请参阅排序文档 (doc sort)。

关于matlab - 如何将属性 _value 对添加到 txt 文件并按字母顺序对它们进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14390517/

相关文章:

matlab - 在 MATLAB 中计算组均值/中位数,其中组 ID 在单独的列中

user-interface - 添加滚动条到带有子图的图形

matlab - norminv 和normrnd 之间有什么关系?

matlab - 查找 matlab 命令窗口句柄/引用

matlab - 计算表中某列的某些行的平均值

arrays - 如果满足特定条件,则删除数组行

arrays - 查找两个数组之间的(多集)差异

matlab - 以编程方式打包 MATLAB 工具箱

matlab - 了解 Matlab 中的过滤器

c++ - 如何在 MATLAB 中编写 max(abs)