首页 > 解决方案 > 可写逗号而不是点,用于小数点分隔符

问题描述

我将数据作为 Matlab 数字存储在“.mat”文件中,并且我想将该表漂亮地打印为 tsv 文件,但是我想将小数点替换为十进制逗号。

这是我尝试过的。

measurements=load('Emeasured.mat');
writetable(struct2table(measurements),"messwertelab1.tsv",'Delimiter','\t')

有效,但数字“难看”,我会使用类似的工具,num2eng但我需要对其进行本地化,以便小数点分隔符是逗号。

nf=java.text.NumberFormat.getInstance(java.util.Locale.GERMAN);
nformat=@(num)(nf.format(num))
arrayfun(nformat,measurements.column) 

好像有错误,错误信息是。

无法分配到索引 1 处的统一输出数组 2。将“UniformOutput”设置为 false。

原因:
索引超出 Java 数组维度

为什么我不能遍历数组? nformat(2.124)给出期望值。

标签: matlab

解决方案


以下是如何writetable使用逗号作为小数点分隔符而不是点来将输出写入带有数字的表格。

df=java.text.DecimalFormat("0", ...
    java.text.DecimalFormatSymbols.getInstance(java.util.Locale.GERMAN));
%or the locale you desire.
df.setMaximumFractionDigits(340);
%340 is the maximum Value.
%When calling the function arrayfun with `UniformOutput` to false
%you get a collection of cells back, this is how to flatten them to a list of strings
vflatten = @(x, varargin) vertcat(x{:});
%This is the callback for arrayfun
nformat=@(num)(string(df.format(num)));
%Finally your formatted arrays.
vflatten(arrayfun(nformat,yourArrayWithNumbers,"UniformOutput",false));

推荐阅读