How to rename table variables in Matlab?

3.1k Views Asked by At

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.

1

There are 1 best solutions below

7
On BEST ANSWER

Maybe you mistyped your question's code, but it seems that you are using the array xaxis instead of C. Anyway, I suggest you try casting the cells content to strings this way

T = array2table(R,'RowNames', concentration,'VariableNames',cellfun(@(x)num2str(x),num2cell(xaxis),'uniformoutput',false));

Edit

Looking through the table docs it says that

Variable names, specified as the comma-separated pair consisting of 'VariableNames' and a cell array of character vectors that are nonempty and distinct. The number of character vectors must equal the number of variables. The variable names that you assign must be valid MATLAB® variable names. You can determine valid variable names using the function isvarname.

Furthermore, the isvarname function says that

A valid variable name begins with a letter and contains not more than namelengthmax characters. Valid variable names can include letters, digits, and underscores. MATLAB keywords are not valid variable names. To determine if the input is a MATLAB keyword, use the iskeyword function.

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 following

T = array2table(R,'RowNames', concentration,...
    'VariableNames',cellfun(@(x)['wavel_',regexprep(num2str(x),'.','_')],num2cell(xaxis),'uniformoutput',false));

This 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. The array2table function's doc says that if you don't supply the 'variableNames' option it will do the following:

If valid MATLAB identifiers are not available for use as variable names, MATLAB uses a cell array of N character vectors of the form {'Var1' ... 'VarN'} where N is the number of variables.

Maybe using the default variable names is good enough.