Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Matlab Append new columns into Excel

  1. Jun 20, 2016 #1

    kelvin490

    User Avatar
    Gold Member

    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.
     
  2. jcsd
  3. Jun 20, 2016 #2

    jedishrfu

    Staff: Mentor

  4. Jun 20, 2016 #3

    kelvin490

    User Avatar
    Gold Member

    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Append new columns into Excel
  1. Excel - (Replies: 2)

  2. Excel inputs. (Replies: 7)

Loading...