Suppose you have the following data:
A = [1,2,3;4,5,6];
headers = {'force', 'mass', 'acceleration'};
units = {'N','Kg','m/s^2'};
Let's say I want to convert it to a table, where headers
will be the 'VariableNames'
:
table_of_data = cell2table([units; num2cell(A)]);
table_of_data.Properties.VariableNames = headers
table_of_data =
force mass acceleration
_____ ____ ____________
N 'Kg' 'm/s^2'
[2] [3]
[5] [6]
Note that the first two columns of A
are removed. This is because MATLAB treats the single character N
differently than 'Kg'
and 'm/s^2'
. If I insert a space after 'N '
I get:
table_of_data =
force mass acceleration
_____ ____ ____________
'N ' 'Kg' 'm/s^2'
[1] [2] [3]
[4] [5] [6]
How can I get a proper table, with all elements displayed without inserting a space 'N '
?
It's no problem to use a single character in units
if I add more rows to the cell array, such as [headers; units; num2cell(A)]
, so the following works:
table_of_data = cell2table([headers; units; num2cell(A)]);
table_of_data(1,:) = [];
table_of_data.Properties.VariableNames = headers
table_of_data =
force mass acceleration
_____ ____ ____________
'N ' 'Kg' 'm/s^2'
[1] [2] [3]
[4] [5] [6]
How can I solve this without turning to cumbersome workarounds?