MATLAB How can I append new columns to an existing Excel file with MATLAB?

AI Thread Summary
To append new columns to an existing Excel file using MATLAB without altering the original data, one can utilize the xlswrite function. The process involves reading the existing data to determine the current number of columns, then calculating the position for the new data. The xlcolumnletter function can convert the column index to the appropriate letter format for Excel. An alternative method suggested is exporting the file to CSV, appending data using a scripting language like Python or awk, and then reimporting it. This approach allows for flexibility when dealing with varying row counts in the new data.
kelvin490
Gold Member
Messages
227
Reaction score
3
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.
 
Physics news on Phys.org
  • Like
Likes kelvin490
jedishrfu said:
What about exporting the spreadsheet to a CSV file format and then using a scripting language like awk or python to append data to the end of each line and then reimport the spreadsheet?

and from MATLAB a similar approach:

https://www.mathworks.com/matlabcentral/newsreader/view_thread/274697

and using xlswrite in matlab:

http://www.mathworks.com/matlabcent...y-matlab-matrix-when-i-write-it-to-excel-in-m

Finally I solve it in the following way:

if (step==1)
xlswrite(filename,array,sheetname,'A1'); %Create the file
else

[~,~,Data]=xlsread(filename,sheetname); %read in all the old data

OriCol=size(Data,2); %get the column number of the old data

NewCol=OriCol+1; %the new array is placed right next to the original data

ColLetter=xlcolumnletter(NewCol);

StartCell=[ColLetter,'1'];

xlswrite(filename,array,sheetname,StartCell);

end

The xlcolumnletter function is found here:
http://www.mathworks.com/matlabcentral/answers/54153-dynamic-ranges-using-xlswrite
 
Back
Top