How do I create a table and add a row of text from an existing matrix?

17 visualizzazioni (ultimi 30 giorni)
I have a large 7000 x 125 matrix. The matrix was created from doing a 'readcell' operation to pull data from excel. I have since manipulated that data within matlab to a point where I can't go back and just do 'readtable' instead. Therefore, I want to create an exportable table that has the heading for each of the columns. I am willing to type out the heading for each column 125 times, but I am open to ideas on how to pull just the column headings from excel and attach them to the new table in Matlab. I would provide some code but it would just be a readfile and the matrix so feel free to generate any examples with a smaller matrix if necessary. I can provide some code if needed however.
  2 Commenti
the cyclist
the cyclist il 3 Feb 2023
It would actually be better if you uploaded a few rows (including) header of the original Excel file, for testing solutions. There is enough variation in how things are stored, that can make for surprisingly different methods to doing the data manipulation.
Is the matrix you have stored in MATLAB just a double-precision numeric array, or some other data type?
Kendell
Kendell il 3 Feb 2023
@the cyclist It is just a double numeric array. I took out all of the non numeric values.

Accedi per commentare.

Risposte (2)

Sulaymon Eshkabilov
Sulaymon Eshkabilov il 3 Feb 2023
Here is one of the possible solutions with strcat() to rename the table header variable names, e.g.:
DAT = randi(13, 15);
RR = array2table(DAT) % Table header var. names are: DAT1, DAT2, DAT3 ... DAT15
RR = 15×15 table
DAT1 DAT2 DAT3 DAT4 DAT5 DAT6 DAT7 DAT8 DAT9 DAT10 DAT11 DAT12 DAT13 DAT14 DAT15 ____ ____ ____ ____ ____ ____ ____ ____ ____ _____ _____ _____ _____ _____ _____ 7 2 10 7 11 9 10 4 10 10 4 13 2 6 7 11 2 5 7 2 1 2 10 2 13 8 6 8 3 12 9 10 13 7 5 12 5 4 11 11 11 6 1 12 5 4 3 12 10 4 8 13 1 5 10 3 7 7 6 2 7 9 5 5 2 11 10 12 5 13 10 6 11 12 8 5 8 8 11 1 6 7 6 3 11 9 1 12 6 7 3 5 3 1 13 10 5 2 3 5 13 4 3 1 10 8 11 10 6 12 7 11 5 5 12 7 4 4 9 6 7 5 9 3 7 3 5 9 1 1 13 12 8 1 9 1 11 10 8 7 8 12 11 4 3 8 11 12 13 5 4 2 3 13 2 8 8 13 13 13 3 2 3 12 2 3 1 7 9 3 2 7 11 13 6 13 4 13 12 4 3 10 3 13 7 11 5 5 10 2 10 5 12 1 10 6 7 7 13 2 4 1 6 8 10 7 11 11 12 9 8 9 11 3 7 9 8 6 5 4 4 10 10 4 13
for ii = 1:length(DAT)
OLD{ii} = strcat('DAT', num2str(ii));
NEW{ii}=strcat('X', num2str(ii));
end
RR = renamevars(RR, OLD, NEW) % Renamed by new variable names: X1, X2, X3, ... X15
RR = 15×15 table
X1 X2 X3 X4 X5 X6 X7 X8 X9 X10 X11 X12 X13 X14 X15 __ __ __ __ __ __ __ __ __ ___ ___ ___ ___ ___ ___ 7 2 10 7 11 9 10 4 10 10 4 13 2 6 7 11 2 5 7 2 1 2 10 2 13 8 6 8 3 12 9 10 13 7 5 12 5 4 11 11 11 6 1 12 5 4 3 12 10 4 8 13 1 5 10 3 7 7 6 2 7 9 5 5 2 11 10 12 5 13 10 6 11 12 8 5 8 8 11 1 6 7 6 3 11 9 1 12 6 7 3 5 3 1 13 10 5 2 3 5 13 4 3 1 10 8 11 10 6 12 7 11 5 5 12 7 4 4 9 6 7 5 9 3 7 3 5 9 1 1 13 12 8 1 9 1 11 10 8 7 8 12 11 4 3 8 11 12 13 5 4 2 3 13 2 8 8 13 13 13 3 2 3 12 2 3 1 7 9 3 2 7 11 13 6 13 4 13 12 4 3 10 3 13 7 11 5 5 10 2 10 5 12 1 10 6 7 7 13 2 4 1 6 8 10 7 11 11 12 9 8 9 11 3 7 9 8 6 5 4 4 10 10 4 13

the cyclist
the cyclist il 3 Feb 2023
% Pull the data as a table (even though we are only going to use it to get the header row)
tbl = readtable("TestFile.xlsx");
% Here is your processed data, which is now a numeric
M = rand(7,3);
% Put the numeric data into a table, with the headers
tableM = array2table(M,'VariableName',tbl.Properties.VariableNames)
tableM = 7×3 table
Animal Vegetable Mineral ________ _________ ________ 0.43863 0.45081 0.044159 0.72264 0.15409 0.60169 0.63566 0.84021 0.67586 0.075885 0.78282 0.041741 0.29343 0.58829 0.26808 0.72577 0.11832 0.24541 0.13893 0.1123 0.69271

Prodotti


Release

R2022b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by