Append new columns into Excel with MATLAB
41 次查看(过去 30 天)
显示 更早的评论
I would like to ask how to use MATLAB to append new columns into existing excel file without altering the original data in the file? In my case I don't know the original number of columns and rows in the file and it is inefficient to open the files one by one and check in practice. Another difficulty is that the new columns may have different number of rows to the existing data so that I cannot use the trick of reading in the data, forming a new matrix and replace the data with the new matrix.
I have seen many posts teaching people how to add new rows but adding new column seems quite a different thing since the columns are named by letters instead of numbers.
Thank you.
1 个评论
Karan Gill
2016-6-21
If you read the sheet in as a table, then perhaps the "join" and "outerjoin" functions might help.
回答(1 个)
Shameer Parmar
2016-6-22
Kelvin,
In order to append new column with existing data of excel, you need to know the no. of rows. Because if you have variable ' data' with 5x10 size and new column c with size 6x1, then it will throw an error.. The c should be 5x1, because the no. of rows is 5.
Let us consider you have excel file 'ABC.xlsx' which have data of 5 rows and 10 column, so first read that excel file as follows:
[d1,d2, existingData] = xlsread('ABC.xlsx');
'existingData' variable will have complete excel data..
now.. the number of rows will be calculated as..
numberOfRow = size(existingData,1);
now the new column should have size (numberOfRow x 1), then only you can join it..
let us consider 'newColumn' is the variable having new data which you want to join with existing data and the size of it is (numberOfRow x 1)
so, do this..
newData = [existingData, newColumn]; % to append the new column with existing data.
xlswrite('ABC.xlsx', newData, 'Sheet1', 'A1'); % to write new data into excel sheet.
winopen('ABC.xlsx'); % to open excel file, just to check.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!