Append new columns into Excel with MATLAB

조회 수: 82 (최근 30일)
Kelvin
Kelvin 2016년 6월 21일
댓글: syahdan edy murad 2018년 7월 6일
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
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
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.
  댓글 수: 1
syahdan edy murad
syahdan edy murad 2018년 7월 6일
Could you give me the complete file matlab code?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by