MATLAB: How to enforce the value of table variables

columnsformatsMATLABtablevariable

Best Answer

You can do this by creating a new table with the 'VariableTypes' property, then copying over each column of the original table.
First, create a helper function to change your formats into types:
function out = convertToType(in)
if in == '%s'
out = 'string';
elseif in == '%f'
out = 'double';
end
end
Then,
% create a new table with the variable types converted
T2 = table('Size', size(T), 'VariableNames', T.Properties.VariableNames,...
'VariableTypes', cellfun(@convertToType,varFormats,'UniformOutput',false))
% copy into the new table
for i = 1:size(T2,2)
T2(:,i) = T(:,i);
end
T2