How do you write an excel sheet, and add input to it everytime program is ran?

I'm having trouble trying to write an excel spreadsheet that starts with 3 rows of data, but every time the program is ran, new data is input by user and needs to be added to the spreadsheet. how do i do this?

 Risposta accettata

Brute-force approach
[~,~,C] = xlsread('file.xls');
<do stuff to create new data>
<make cell array C2 out of new data>
C = [C;C2];
xlswrite('file.xls',C);
If this is likely to get unwieldy, you could either store information on the number of rows somewhere (like a MAT-file), then use xlswrite to write into a particular range, or you could dive into lower-level ActiveX commands to read/write to Excel.

6 Commenti

this would work, but the program has to make the excel sheet too, it cant be premade.
Have you tried the code suggested by Matt?
xlswrite will create a standard excel file with Sheet1,Sheet2 and Sheet3 and will write on the first one if not specified any different one.
Or you can write on a differently named sheet as:
xlswrite('tryThisOut.xlsx',1,'myName')
Ok, so add "if exist('file.xls','file')" before the xlsread command, with an "else C = {};"
It works, thanks so much!
Can you please add more to this answer to elaborate this program.
Suppose i generate some data and i wish to add it to a file
every time i run the code over the image.
then what i need to do.
I mean to say that data must be appended to the same file not overwrite.

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by