Dynamic variable names for MATLAB table
11 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jay Vaidya
el 16 de Nov. de 2020
I am making the header names of the table as following:
colnames = [arrayfun(@(x) sprintf('type%d', x), 1:size(all_sorted), 'UniformOutput', false) arrayfun(@(x) sprintf('binary%d', x), 1:size(all_sorted), 'UniformOutput', false) 'result']
a_table = table(result_matrix, 'VariableNames',cell2mat(strcat({str2mat(colnames)})))
But always the last command gives error about
Error using table (line 259)
Invalid parameter name. Parameter name must be a nonempty string or character vector.
I tried a few permutations and combinations of converting the colnames to string, cell2mat, etc. But they give some or the other error. How can I specify the colnames with the above commands or with any of your other suggestions?
1 comentario
Ameer Hamza
el 16 de Nov. de 2020
The statement 1:size(all_sorted) can be confusing and may not mean what you think it means. Explicitly specifying the dimension will be helpful.
Respuesta aceptada
Stephen23
el 16 de Nov. de 2020
Editada: Stephen23
el 16 de Nov. de 2020
- Use array2table instead of table (the wrong function, unless you really want the array in one variable).
- Get rid of all of that cell2mat and strcat and str2mat confusion (what are you concatenating? Nothing).
all_sorted = [2;3;5];
result_matrix = randi(5,7)
nr = size(all_sorted,1);
C1 = arrayfun(@(x) sprintf('type%d', x), 1:nr, 'UniformOutput', false)
C2 = arrayfun(@(x) sprintf('binary%d', x), 1:nr, 'UniformOutput', false)
VN = [C1,C2,{'result'}];
T = array2table(result_matrix, 'VariableNames',VN)
0 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Data Type Conversion en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!