Wednesday, April 28, 2010

xlswrite: output data to excel file {MATLAB functions}

Sometime when a column or a row of data is too long, you can't copy it from MATLAB and then paste directly into an Excel file. A much simpler way to do this is to use the xlswrite function. Just add a line at the end of your code:

xlswrite ('FileName.xls', VariableName)

If you want to put the data into sheet2 in the file, one more input argument is needed:

xlswrite ('FileName.xls', VariableName, 2)

Pretty easy, right?

11 comments:

  1. Thanks for your comment, Anonymous!

    ReplyDelete
  2. hey do u know how to create a for loop and export to different filenames each time the for loop is repeated?

    is the following goin to work?

    for i =1
    xlswrite(i.xlsx)
    end

    ReplyDelete
  3. I think you use the for loop to repeatedly write/save/export xlsx files. However, you need to pay a little more attention to the file name.
    The (i.xlsx) won't work since i is an integer, not a string.

    ReplyDelete
  4. Hello, I am dealing with large quantity of data with the number of rows exceeding Excel's 65526 row limit. I need to export the data into an Excel spreadsheet.

    I want to write a Matlab code which would break down my data into 65526 rows and save them each as a different variable and then save each variable in a new sheet.

    But I am stuck at defining the row range! Could I set my row range as a variable such as this??

    ---------------------------------------------

    matlab_row=size(t0,1);
    excel_row=65526;

    diff=matlab_row - excel_row;
    size1=matlab_row-65000;

    page1=t0(1:size1,:);

    if diff<0
    data1=XLSWRITE('C:\data1.xls',page1,'Sheet1');

    ------------------

    I am new with VBA programming, all help is appreciated.

    Thanks

    ReplyDelete
    Replies
    1. *** I am new with ***C/C++ programming

      Delete
  5. lets say i want to add 2 vectors....one x and other say y=sin(x) where both x and y are vectors..in to the same excel sheet so that i can create plots of the y vs x kind...so what would i have to change in the command that you have given?

    ReplyDelete
    Replies
    1. i got it... first do x=x'; and y =y'; so as to make x and y column vectors. then create a matrix z= [x y]; and then the command becomes

      xlswrite('xyz.xls',z,Sheet1,'F5'); that takes care of both the vectors otherwise u have to type out the command twice for each of them (x & y).

      Delete
  6. how can i adjust the column width of the cells i write into using xlswrite?

    ReplyDelete
  7. give more descriptive answers with examples

    ReplyDelete

Any comments?