I would like to create a table from a matrix of spectra data, using a specific list of variables (column, which here correspond to 1000 spectral wavelenght values) instead of putting the name manually.
To do so, i use the array2table
function, and matlab documentation shows that names for rows and label of variables must be put as cell arrays, (and not matrix). So I need to first convert my x-axis (spectral wavelenght) to a cell array. I use the following:
C = num2cell(xaxis);
% to convert into a cell array (each cell contain 1 value)
isvarname C
% to check that the variable is valid as a cell array
T = array2table(R,'RowNames', concentration,'VariableNames',C);
Here: R is the matrix, concentration is a Cell array 1x500, xaxis is the wavelenght of spectral data 1x1000 (which is ranged from 600 to 1800, approx. there is no null value).
Unfortunately, I got the following error: "Error using array2table (line 62) The VariableNames property must be a cell array, with each element containing one nonempty string."
which means I can input properly the columns (variable) names (while row names, however, works fine).
Note: i tried the
T.Properties.VariableNames = c
but it is not working either.
I checked the other post on table name value but its not helping, Any thought about this? Thank you very much.
Maybe you mistyped your question's code, but it seems that you are using the array
xaxis
instead ofC
. Anyway, I suggest you try casting the cells content to strings this wayEdit
Looking through the table docs it says that
Furthermore, the isvarname function says that
This means that you cannot use the
xaxis
values as variable names by themselves, you need to, at least, prepend a character and remove the decimal dot. You can do so with the followingThis code will prepend 'wavel_' to the numerical string value. It will also replace the dots with underscores using the
regexprep
function. However, it seems like this is really unnecessary because the column names are not really informative. Thearray2table
function's doc says that if you don't supply the'variableNames'
option it will do the following:Maybe using the default variable names is good enough.