How do I create a table and add a row of text from an existing matrix?
조회 수: 17 (최근 30일)
이전 댓글 표시
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
the cyclist
2023년 2월 3일
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?
답변 (2개)
Sulaymon Eshkabilov
2023년 2월 3일
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
the cyclist
2023년 2월 3일
% 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
참고 항목
카테고리
Help Center 및 File Exchange에서 Database Toolbox에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!