问题描述
基于这篇博文 (Table fields in format in PDF report generator - Matlab),我在一张桌子上运行脚本。在脚本中,我提取一列,在其上运行该函数,然后用相同但复制的表(原始表)中的新列替换旧列。奇怪的是,我发现修改后的列数据没有正确存储在新表中。为什么?我怀疑这是因为分类。我该如何解决?
代码:
clc;
clear all;
dataInput = webread('https://people.sc.fsu.edu/~jburkardt/data/csv/hw_25000.csv');
tempCol = (f(table2array(dataInput(:,2))));
newDataInput = dataInput;
newDataInput(:,2) = table(tempCol);
function FivDigsstr = f(x)
%formatting to character array with 5 significant digits and then splitting.
%at each tab. categorical is needed to remove ' 's that appear around char
%in the output PDF file with newer MATLAB versions
%e.g. with R2018a,there are no ' ' in the output file but ' ' appears with R2020a
FivDigsstr = categorical(split(sprintf('%0.5G\t',x)));
%removing the last (<undefined>) value (which is included due to \t)
FivDigsstr = FivDigsstr(1:end-1);
end
解决方法
您不能用其他类型的值替换一种类型的值。相反,只需重写该列。即使用:
newDataInput.(newDataInput.Properties.VariableNames{2}) = tempCol;
代替:
newDataInput(:,2) = table(tempCol);