matlabmatlab-table

Table fields digit change when replacing data of different types


Based on this post (Table fields in format in PDF report generator - Matlab), I run the script on a table. In the script, I extract one column, run the function on it, and replace the old column with this new one in identical but copied table (of the original). Strangely, I get that the modified column data is not stored correctly in the new table. Why? I suspect that is it is because of the categorical. How can I fix it?

Code:

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

Solution

  • You cannot replace values of one type with values of some other type. Instead, just rewrite the column. i.e use:

    newDataInput.(newDataInput.Properties.VariableNames{2}) = tempCol;
    

    instead of:

    newDataInput(:,2) = table(tempCol);