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:
Writing from MatLab to Excel

Subject: Writing from MatLab to Excel

From: m. k

Date: 21 Sep, 2007 16:30:10

Message: 1 of 10

I have a question and I hope that somebody can help me.

I am building a program that writes a value to a specific
column and row in an existing Excel document.

I was wondering if anybody knows how to use the xlswrite
cmd in MatLab to write to a specific spot in Excel. As it
is now, the xlswrite function allows you to specify a
specific cell (i.e. A1, B3, C5...), however I wish to write
a value from MatLab into a specific column in Excel, as
specified by a variable in a for loop.

for example.

for i = 1:9
...% logic operations here
xlswrite('file name',[1],'sheet','range')
end

But for the range, instead of putting something
like 'A1:M2' or 'M3', I want to write to a cell that is
specified by the i count in the for loop (ideally, if it
were in a MatLab array, i would have 'range' = Excel(r,i)
where Excel is the array written to, and (r,i) the position
in the array to write to)

Does anybody know how to do this? If my writing is not
clear, please let me know and I'll try to clarify.

Thanks.

Subject: Writing from MatLab to Excel

From: Peter Boettcher

Date: 21 Sep, 2007 18:36:45

Message: 2 of 10

"m. k" <mkawika@mathworks.com> writes:

> I have a question and I hope that somebody can help me.
>
> I am building a program that writes a value to a specific
> column and row in an existing Excel document.
>
> I was wondering if anybody knows how to use the xlswrite
> cmd in MatLab to write to a specific spot in Excel. As it
> is now, the xlswrite function allows you to specify a
> specific cell (i.e. A1, B3, C5...), however I wish to write
> a value from MatLab into a specific column in Excel, as
> specified by a variable in a for loop.
>
> for example.
>
> for i = 1:9
> ...% logic operations here
> xlswrite('file name',[1],'sheet','range')
> end
>
> But for the range, instead of putting something
> like 'A1:M2' or 'M3', I want to write to a cell that is
> specified by the i count in the for loop (ideally, if it
> were in a MatLab array, i would have 'range' = Excel(r,i)
> where Excel is the array written to, and (r,i) the position
> in the array to write to)

'sheet' and 'range' are both specified as strings. You need to build
those strings dynamically. For instance:

sheet = 'Excel';
i = 17;
range = sprintf('A%i', i);

range will be a string equal to 'A17'. Now

xlswrite('filename', [1], sheet, range)

calls the function with the strings you've just built.

-Peter

Subject: Writing from MatLab to Excel

From: m. k

Date: 21 Sep, 2007 20:08:47

Message: 3 of 10

Thanks Peter. Now i know how to generate it dynamically.
However, I have a follow-up question. I need to generate
the range dynamically in the rows AND column directions.
If I am not mistaken, the cmd line you gave me generates
the range dynamically only in rows. Do you know how I can
generate it dynamically in the column direction as well? I
found it difficult because ExcelA1 notation uses letters
(A,B,C...) to denote their columns as opposed to numbers,
and I do not know how to remedy that.

Thanks
-m

Peter Boettcher <boettcher@ll.mit.edu> wrote in message
<muyps0b7tmq.fsf@G99-Boettcher.llan.ll.mit.edu>...
> "m. k" <mkawika@mathworks.com> writes:
>
> > I have a question and I hope that somebody can help me.
> >
> > I am building a program that writes a value to a
specific
> > column and row in an existing Excel document.
> >
> > I was wondering if anybody knows how to use the
xlswrite
> > cmd in MatLab to write to a specific spot in Excel. As
it
> > is now, the xlswrite function allows you to specify a
> > specific cell (i.e. A1, B3, C5...), however I wish to
write
> > a value from MatLab into a specific column in Excel, as
> > specified by a variable in a for loop.
> >
> > for example.
> >
> > for i = 1:9
> > ...% logic operations here
> > xlswrite('file name',[1],'sheet','range')
> > end
> >
> > But for the range, instead of putting something
> > like 'A1:M2' or 'M3', I want to write to a cell that is
> > specified by the i count in the for loop (ideally, if
it
> > were in a MatLab array, i would have 'range' = Excel
(r,i)
> > where Excel is the array written to, and (r,i) the
position
> > in the array to write to)
>
> 'sheet' and 'range' are both specified as strings. You
need to build
> those strings dynamically. For instance:
>
> sheet = 'Excel';
> i = 17;
> range = sprintf('A%i', i);
>
> range will be a string equal to 'A17'. Now
>
> xlswrite('filename', [1], sheet, range)
>
> calls the function with the strings you've just built.
>
> -Peter
>
>
>

Subject: Writing from MatLab to Excel

From: Peter Boettcher

Date: 24 Sep, 2007 13:55:45

Message: 4 of 10

"m. k" <mkawika@mathworks.com> writes:

> Thanks Peter. Now i know how to generate it dynamically.
> However, I have a follow-up question. I need to generate
> the range dynamically in the rows AND column directions.
> If I am not mistaken, the cmd line you gave me generates
> the range dynamically only in rows. Do you know how I can
> generate it dynamically in the column direction as well? I
> found it difficult because ExcelA1 notation uses letters
> (A,B,C...) to denote their columns as opposed to numbers,
> and I do not know how to remedy that.

sprintf('%c', 'A' + i - 1)

should give you a start

-Peter

Subject: Writing from MatLab to Excel

From: Vinz A

Date: 29 Jun, 2010 21:10:16

Message: 5 of 10

Hi Peter Boettcher

Can you please explain what are the notations inside the sprintf statement
 sprintf('%c', 'A' + i - 1)
Even I have the same problem.
It seems to work for one row how do i update for rest of the values.
If I understand then I can try it out.


Cheers
vinz

Subject: Writing from MatLab to Excel

From: TideMan

Date: 29 Jun, 2010 21:57:09

Message: 6 of 10

On Jun 30, 9:10 am, "Vinz A" <vin...@gmail.com> wrote:
> Hi Peter Boettcher
>
> Can you please explain what are the notations inside the sprintf statement
>  sprintf('%c', 'A' + i - 1)
> Even I have the same problem.
> It seems to work for one row how do i update for rest of the values.
> If I understand then I can try it out.
>
> Cheers
> vinz

The best way to learn/understand is to try it out:
sprintf('%c','A'+1)
sprintf('%c','A'+25)
Can't you see what is happening?

Subject: Writing from MatLab to Excel

From: us

Date: 29 Jun, 2010 22:01:21

Message: 7 of 10

TideMan <mulgor@gmail.com> wrote in message <f9f64604-0b0b-41b8-9ea4-0d5789cbfdec@i31g2000yqm.googlegroups.com>...
> On Jun 30, 9:10 am, "Vinz A" <vin...@gmail.com> wrote:
> > Hi Peter Boettcher
> >
> > Can you please explain what are the notations inside the sprintf statement
> >  sprintf('%c', 'A' + i - 1)
> > Even I have the same problem.
> > It seems to work for one row how do i update for rest of the values.
> > If I understand then I can try it out.
> >
> > Cheers
> > vinz
>
> The best way to learn/understand is to try it out:
> sprintf('%c','A'+1)
> sprintf('%c','A'+25)
> Can't you see what is happening?
>

ahhh... NOW i can see what's happening(!)...
thank you so much, tm...

us

Subject: Writing from MatLab to Excel

From: Thomas Meinguet

Date: 23 Apr, 2012 16:42:06

Message: 8 of 10

Did you consider the option to build all your XLS values in a unique Matlab cell array and then write it at once in XLS only specifying the top left cell with a letter?

This way in the cell array you can assign values with (i,j) indices.

This might be a good alternative...

Subject: Writing from MatLab to Excel

From: ImageAnalyst

Date: 23 Apr, 2012 21:11:59

Message: 9 of 10

On Apr 23, 12:42 pm, "Thomas Meinguet" <thomas.meing...@ing.be> wrote:
> Did you consider the option to build all your XLS values in a unique Matlab cell array and then write it at once in XLS only specifying the top left cell with a letter?
>
> This way in the cell array you can assign values with (i,j) indices.
>
> This might be a good alternative...

--------------------------------------------------------
Considering that it was 5 years ago, I doubt that we'll ever find
out. But your method, Thomas, is the one I also use. Plus, if you're
not using ActiveX, it is also faster than calling xlswrite() multiple
times.

Subject: Writing from MatLab to Excel

From: Rinachi Garg

Date: 4 Jun, 2012 22:16:09

Message: 10 of 10

"Thomas Meinguet" <thomas.meinguet@ing.be> wrote in message <jn40ou$i7i$1@newscl01ah.mathworks.com>...
> Did you consider the option to build all your XLS values in a unique Matlab cell array and then write it at once in XLS only specifying the top left cell with a letter?
>
> This way in the cell array you can assign values with (i,j) indices.
>
> This might be a good alternative...

Can you please explain how to do that?

I have a cell of cell arrays and I want to write all the values in a Excel spreadsheet. All the Cell arrays have different dimensions.

I would like to try your method. Thanks.

Tags for 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