Append new columns into Excel with MATLAB

144 views (last 30 days)
Kelvin
Kelvin on 21 Jun 2016
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 Comment
Karan Gill
Karan Gill on 21 Jun 2016
If you read the sheet in as a table, then perhaps the "join" and "outerjoin" functions might help.

Sign in to comment.

Answers (1)

Shameer Parmar
Shameer Parmar on 22 Jun 2016
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.

Categories

Find more on Data Import from MATLAB 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!