How do I create a table and add a row of text from an existing matrix?
9 views (last 30 days)
Show older comments
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 Comments
the cyclist
on 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?
Answers (2)
Sulaymon Eshkabilov
on 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
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
0 Comments
the cyclist
on 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)
0 Comments
See Also
Categories
Find more on Database Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!