微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

替换不同类型数据时表字段数字变化

如何解决替换不同类型数据时表字段数字变化

基于这篇博文 (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);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。