Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Excel writing; next empty cell

Subject: Excel writing; next empty cell

From: Mike

Date: 20 Apr, 2011 20:27:05

Message: 1 of 6

Greetings all!

I have a program that calculates out several values and I want to write them to an Excel data sheet. However, I do not want the data to be overwritten. This is to be a log, so every time the program runs it needs to write to the next blank row.

The solutions I have seen are all for a single program run, and if you run the program again it overwrites the data from the previous run. A program that starts writing at A1 or similar is not what I am looking for.

So I am looking for a solution that will look into the Excel file I am writing into and determine what column is the first that is blank, and then write to that row. I can figure out the remaining data handling myself (hopefully!).

Any ideas, thoughts, or solutions? Any help is greatly appreciated!

-Michael

Subject: Excel writing; next empty cell

From: ImageAnalyst

Date: 20 Apr, 2011 21:12:13

Message: 2 of 6

I believe you're going to have to ask Excel via ActiveX to do that. I
don't know off the top of my head how to find the last cell. You can
get some help by recording a macro in Excel and seeing what it
recorded.

Subject: Excel writing; next empty cell

From: Armindo

Date: 20 Apr, 2011 21:17:05

Message: 3 of 6

You can read first the file and than count the rows using a variable size.
After that you ran write in the file in the right row.

example:

file directory
file = 'C:\Documents and Settings\desktop\rarb.xls'

% store data of file in variable a
a = xlsread(file);

% new data to write in file
t = ones(1,10)

% last row with data in the file
nRows = (size(a,1));

% plus 1 to write in the next line (if you have an header it should be + 2)
nRows = nRows +1;

% convert number to string
b = num2str(nRows)

% if you want to add data to the collum A you make concat strings
c = strcat('A', b)

% right to file the data t on the sheet Folha1 begining in the row c (e.g. A20)
xlswrite(file, t,'Folha1',c);

Subject: Excel writing; next empty cell

From: Jeremy

Date: 20 Apr, 2011 21:29:04

Message: 4 of 6

Or you can download this (http://www.mathworks.com/matlabcentral/fileexchange/28600-xlsappend).

Subject: Excel writing; next empty cell

From: Mike

Date: 25 Apr, 2011 22:09:05

Message: 5 of 6

Thanks for the possible solutions, I will owrk on those and hopefully get somewhere with them.

Subject: Excel writing; next empty cell

From: Rbk

Date: 7 Jun, 2012 12:16:07

Message: 6 of 6

"Armindo" wrote in message <ionigh$fin$1@fred.mathworks.com>...
> You can read first the file and than count the rows using a variable size.
> After that you ran write in the file in the right row.
>
> example:
>
> file directory
> file = 'C:\Documents and Settings\desktop\rarb.xls'
>
> % store data of file in variable a
> a = xlsread(file);
>
> % new data to write in file
> t = ones(1,10)
>
> % last row with data in the file
> nRows = (size(a,1));
>
> % plus 1 to write in the next line (if you have an header it should be + 2)
> nRows = nRows +1;
>
> % convert number to string
> b = num2str(nRows)
>
> % if you want to add data to the collum A you make concat strings
> c = strcat('A', b)
>
> % right to file the data t on the sheet Folha1 begining in the row c (e.g. A20)
> xlswrite(file, t,'Folha1',c);

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us