MATLAB - xlswrite - how to avoid overwriting

13 views (last 30 days)
JOKY JOKE
JOKY JOKE on 17 Jan 2012
Answered: Raghunandan V on 12 Mar 2019
Hi,
I am trying to write values to certain Excel cells. However I only want to fill certain cells and not the whole column.
Lets say, I want to fill A1, A3, A10 and A20 in the Excel sheet. The other cells do either have values or functions, which I do not want to overwrite.
In Matlab I have a cell array defined by: data{1} = 10, data{3} = 20, data{10} = 100, data{20} = 400.
If I now use xlswrite('file.xls', data, 'sheet', 'A1') all other cells in Excel are overwritten with empty cells. So I loose the content of A2, A4 etc.
I can create a loop, where I call xlswrite for every cell, but this takes forever, if I have many cells to write.
Is there an easier way to fill only certain cells of a column?
Thank you

Answers (2)

the cyclist
the cyclist on 17 Jan 2012
I am not 100% sure about this idea, and I cannot test it because I am on a Mac, and xlswrite does not play nicely on a Mac. But, it might be worth a shot if you don't get a better answer.
I believe that the part that takes "forever" is the opening of the Excel file that xlswrite does, not the actual writing of the data. Therefore, looping over the data-writing shouldn't be too bad.
If you type "edit xlswrite", you can see the commands that MATLAB is using to open Excel, and then write. If you are bold, you could commandeer those commands, only open the Excel file once, and put in your own loop for writing data.
It will be kludgy, but I think it will work. I strongly recommend you make a copy of xlswrite to edit, lest you corrupt the original!

Raghunandan V
Raghunandan V on 12 Mar 2019
Hey,
Great question . I was also caught up in the same question but I know have the answer. So the logical thing to do would be to read the data from the excel before writing it.
[Num, CellArray, Other] = xlsread('Data.xlsx');
After you read your data will be in CellArray. Now you can replace the required data by CellArray{1} = 1, CellArray{5}=10 etc and then write it back to excel!!

Community Treasure Hunt

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

Start Hunting!

Translated by