Thread Subject: xlswrite

Subject: xlswrite

From: ragab Rabeiy

Date: 15 Oct, 2009 10:05:03

Message: 1 of 15

i am a new user in Matlab, and i faced small problem, could u please help me??

this problem is: for the following code, Matlab writes only in Excel the final value (pi*5),

and I want it to write all the data from 1 to 5.

for x=1:5;
m=pi*x;
xlswrite('file.xls', m)
end

Subject: xlswrite

From: Branko

Date: 15 Oct, 2009 11:28:04

Message: 2 of 15

"ragab Rabeiy" <r_rabeiy@yahoo.com> wrote in message <hb6s4f$evt$1@fred.mathworks.com>...
> i am a new user in Matlab, and i faced small problem, could u please help me??
>
> this problem is: for the following code, Matlab writes only in Excel the final value (pi*5),
>
> and I want it to write all the data from 1 to 5.
>
> for x=1:5;
> m=pi*x;
> xlswrite('file.xls', m)
> end

Why do you use for loop? No need in this case:
x=1:5;
m=pi*x;
xlswrite('filename.xls', m)

Branko

Subject: xlswrite

From: Nasser Abbasi

Date: 15 Oct, 2009 12:46:04

Message: 3 of 15


"ragab Rabeiy" <r_rabeiy@yahoo.com> wrote in message
news:hb6s4f$evt$1@fred.mathworks.com...
>i am a new user in Matlab, and i faced small problem, could u please help
>me??
>
> this problem is: for the following code, Matlab writes only in Excel the
> final value (pi*5),
>
> and I want it to write all the data from 1 to 5.
>
> for x=1:5;
> m=pi*x;
> xlswrite('file.xls', m)
> end


becuase each time in the loop, you are starting all over and writing to a
new file.xls. The old file.xls is overwritten. there is no append by
default.

As mentiod, no loop is neede, write the whole matrix data in one write. see
help on how to make different sheets if you want.

--Nasser

Subject: xlswrite

From: ragab Rabeiy

Date: 15 Oct, 2009 14:40:22

Message: 4 of 15

"Nasser Abbasi" <nma@12000.org> wrote in message <pQEBm.2947$4E.1354@newsfe08.iad>...
>
> "ragab Rabeiy" <r_rabeiy@yahoo.com> wrote in message
> news:hb6s4f$evt$1@fred.mathworks.com...
> >i am a new user in Matlab, and i faced small problem, could u please help
> >me??
> >
> > this problem is: for the following code, Matlab writes only in Excel the
> > final value (pi*5),
> >
> > and I want it to write all the data from 1 to 5.
> >
> > for x=1:5;
> > m=pi*x;
> > xlswrite('file.xls', m)
> > end
>
>
> becuase each time in the loop, you are starting all over and writing to a
> new file.xls. The old file.xls is overwritten. there is no append by
> default.
>
> As mentiod, no loop is neede, write the whole matrix data in one write. see
> help on how to make different sheets if you want.
>
> --Nasser
>
Thank you for u replay,
indeed it is only xlswrite test, but i am using it in a larger code, and when I removed for...end, I got error. the following is a larger code but not the largest one, also it is part of my original code.

clc
%%part of :convert to wind direction
wd=66;
phi=wd*pi/180;
m=xlsread('data', 'point_data','a2:e20');
x=m(:,3); y=m(:,4); z=m(:,5);
n=size(x,1);
for j=1:n;
xr=x(j,:);
yr=y(j,:);
zr=z(j,:);
xs=3589800 ; ys=5741200; zs=511.05;
X=(xr-xs); Y=(yr-ys);
%% convert to wind direction coordinates
theta=atan((xr-xs)/(yr-ys));
x_=((xr-xs)/sin(theta))*cos(phi-theta);
y_=((xr-xs)/sin(theta))*sin(phi-theta);
   C=x_*.25+y_*.025;
     xlswrite ('test.xls',C)
end

the same problem i got only the last C value in the excel sheet. could you help in it please.

Subject: xlswrite

From: ragab Rabeiy

Date: 15 Oct, 2009 14:42:03

Message: 5 of 15

"Branko " <bogunovic@mbss.org> wrote in message <hb7104$dus$1@fred.mathworks.com>...
> "ragab Rabeiy" <r_rabeiy@yahoo.com> wrote in message <hb6s4f$evt$1@fred.mathworks.com>...
> > i am a new user in Matlab, and i faced small problem, could u please help me??
> >
> > this problem is: for the following code, Matlab writes only in Excel the final value (pi*5),
> >
> > and I want it to write all the data from 1 to 5.
> >
> > for x=1:5;
> > m=pi*x;
> > xlswrite('file.xls', m)
> > end
>
> Why do you use for loop? No need in this case:
> x=1:5;
> m=pi*x;
> xlswrite('filename.xls', m)
>
> Branko

Thank you for u replay,
indeed it is only xlswrite test, but i am using it in a larger code, and when I removed for...end, I got error. the following is a larger code but not the largest one, also it is part of my original code.

clc
%%part of :convert to wind direction
wd=66;
phi=wd*pi/180;
m=xlsread('data', 'point_data','a2:e20');
x=m(:,3); y=m(:,4); z=m(:,5);
n=size(x,1);
for j=1:n;
xr=x(j,:);
yr=y(j,:);
zr=z(j,:);
xs=3589800 ; ys=5741200; zs=511.05;
X=(xr-xs); Y=(yr-ys);
%% convert to wind direction coordinates
theta=atan((xr-xs)/(yr-ys));
x_=((xr-xs)/sin(theta))*cos(phi-theta);
y_=((xr-xs)/sin(theta))*sin(phi-theta);
   C=x_*.25+y_*.025;
     xlswrite ('test.xls',C)
end

the same problem i got only the last C value in the excel sheet. could you help in it please.

Subject: xlswrite

From: ImageAnalyst

Date: 15 Oct, 2009 14:59:17

Message: 6 of 15

ragab Rabeiy
Just use sprintf() to create a brand new filename each iteration of
the loop and then write out that uniquely-named file, rather than
overwriting the same file ("test.xls") each time. For example

baseFileName = sprintf('Test %d.xls', j);
folder = 'c:\my output folder';
if ~exist(folder, 'dir')
    mkdir(folder);
end
fullFileName = fullfile(folder, baseFileName);
xlswrite(fullFileName);

Regards,
ImageAnalyst

Subject: xlswrite

From: ragab Rabeiy

Date: 15 Oct, 2009 16:59:05

Message: 7 of 15

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <6d40011a-0172-4478-b67c-04c9e03d6aac@a31g2000yqn.googlegroups.com>...
> ragab Rabeiy
> Just use sprintf() to create a brand new filename each iteration of
> the loop and then write out that uniquely-named file, rather than
> overwriting the same file ("test.xls") each time. For example
>
> baseFileName = sprintf('Test %d.xls', j);
> folder = 'c:\my output folder';
> if ~exist(folder, 'dir')
> mkdir(folder);
> end
> fullFileName = fullfile(folder, baseFileName);
> xlswrite(fullFileName);
>
> Regards,
> ImageAnalyst

thank you ImageAnalyst,
your example seems to be helpful, but i can not understand it. I tried it many times but gave may error also. could you please write a simple one , by which i can make copy and past in command window and it works directly, like

????
for n=1:5,
m=n*pi
end
xlaswrite.......????

regards
ragab

Subject: xlswrite

From: ImageAnalyst

Date: 15 Oct, 2009 20:20:46

Message: 8 of 15

On Oct 15, 12:59 pm, "ragab Rabeiy" <r_rab...@yahoo.com> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <6d40011a-0172-4478-b67c-04c9e03d6...@a31g2000yqn.googlegroups.com>...
> > ragab Rabeiy
> > Just use sprintf() to create a brand new filename each iteration of
> > the loop and then write out that uniquely-named file, rather than
> > overwriting the same file ("test.xls") each time.  For example
>
> > baseFileName = sprintf('Test %d.xls', j);
> > folder = 'c:\my output folder';
> > if ~exist(folder, 'dir')
> >     mkdir(folder);
> > end
> > fullFileName = fullfile(folder, baseFileName);
> > xlswrite(fullFileName);
>
> > Regards,
> > ImageAnalyst
>
> thank you ImageAnalyst,
> your example seems to be helpful, but i can not understand it. I tried it many times but gave may error also. could you please write a simple one , by which i can make copy and past in command window and it works directly, like
>
> ????
> for n=1:5,
> m=n*pi
> end
> xlaswrite.......????
>
> regards
> ragab- Hide quoted text -
>
> - Show quoted text -

------------------------------------------------------------------------------------------------------------------------------------------
Hmmmmm.... It's really not that hard. Nonetheless, I did this code
for you:
folder = 'C:\Program Files\MATLAB\work';
for loopIndex = 1:3
baseFileName = sprintf('Test %d.xls', loopIndex);
if ~exist(folder, 'dir')
mkdir(folder);
end
dataArray = rand(20, 10);
fullFileName = fullfile(folder, baseFileName);
xlswrite(fullFileName, dataArray);
end

It works, though it's very slow. You'd be better off using xlswrite1
() from the File Exchange so that you don't have to open and close
Excel at every loop iteration.
Good luck,
ImageAnalyst
P.S. Above you put the xlswrite outside the loop but in your earlier
posting you had it inside the loop, so I'm not sure what's up with
that.

Subject: xlswrite

From: ragab Rabeiy

Date: 16 Oct, 2009 08:01:33

Message: 9 of 15

ImageAnalyst <imageanalyst@mailinator.com> wrote in message <5e9ea19c-a0c8-4ea8-a8ce-41222bdbe88f@y21g2000yqn.googlegroups.com>...
> On Oct 15, 12:59?pm, "ragab Rabeiy" <r_rab...@yahoo.com> wrote:
> > ImageAnalyst <imageanal...@mailinator.com> wrote in message <6d40011a-0172-4478-b67c-04c9e03d6...@a31g2000yqn.googlegroups.com>...
> > > ragab Rabeiy
> > > Just use sprintf() to create a brand new filename each iteration of
> > > the loop and then write out that uniquely-named file, rather than
> > > overwriting the same file ("test.xls") each time. ?For example
> >
> > > baseFileName = sprintf('Test %d.xls', j);
> > > folder = 'c:\my output folder';
> > > if ~exist(folder, 'dir')
> > > ? ? mkdir(folder);
> > > end
> > > fullFileName = fullfile(folder, baseFileName);
> > > xlswrite(fullFileName);
> >
> > > Regards,
> > > ImageAnalyst
> >
> > thank you ImageAnalyst,
> > your example seems to be helpful, but i can not understand it. I tried it many times but gave may error also. could you please write a simple one , by which i can make copy and past in command window and it works directly, like
> >
> > ????
> > for n=1:5,
> > m=n*pi
> > end
> > xlaswrite.......????
> >
> > regards
> > ragab- Hide quoted text -
> >
> > - Show quoted text -
>
> ------------------------------------------------------------------------------------------------------------------------------------------
> Hmmmmm.... It's really not that hard. Nonetheless, I did this code
> for you:
> folder = 'C:\Program Files\MATLAB\work';
> for loopIndex = 1:3
> baseFileName = sprintf('Test %d.xls', loopIndex);
> if ~exist(folder, 'dir')
> mkdir(folder);
> end
> dataArray = rand(20, 10);
> fullFileName = fullfile(folder, baseFileName);
> xlswrite(fullFileName, dataArray);
> end
>
> It works, though it's very slow. You'd be better off using xlswrite1
> () from the File Exchange so that you don't have to open and close
> Excel at every loop iteration.
> Good luck,
> ImageAnalyst
> P.S. Above you put the xlswrite outside the loop but in your earlier
> posting you had it inside the loop, so I'm not sure what's up with
> that.
Hello ImageAnalyst, your code is working, thank you.
but it gives me three exile files.

When i made the attached code, it gave me 50 files, each one has its iteration value. so, i need all the results to be in a unique file also in a unique sheet.
May you have a suggestion.

 
folder ='D:\work';
for n=1:50;
    File=sprintf('Test%d.xls',n);
    if~exist(folder,'dir'); mkdir(folder); end
    m=n*pi;
    fileName=fullfile(folder,File);
    xlswrite(fileName,m);
end

Regards
Ragab

Subject: xlswrite

From: ImageAnalyst

Date: 16 Oct, 2009 11:55:24

Message: 10 of 15

On Oct 16, 4:01 am, "ragab Rabeiy" <r_rab...@yahoo.com> wrote:
> ImageAnalyst <imageanal...@mailinator.com> wrote in message <5e9ea19c-a0c8-4ea8-a8ce-41222bdbe...@y21g2000yqn.googlegroups.com>...
> > On Oct 15, 12:59?pm, "ragab Rabeiy" <r_rab...@yahoo.com> wrote:
> > > ImageAnalyst <imageanal...@mailinator.com> wrote in message <6d40011a-0172-4478-b67c-04c9e03d6...@a31g2000yqn.googlegroups.com>...
> > > > ragab Rabeiy
> > > > Just use sprintf() to create a brand new filename each iteration of
> > > > the loop and then write out that uniquely-named file, rather than
> > > > overwriting the same file ("test.xls") each time. ?For example
>
> > > > baseFileName = sprintf('Test %d.xls', j);
> > > > folder = 'c:\my output folder';
> > > > if ~exist(folder, 'dir')
> > > > ? ? mkdir(folder);
> > > > end
> > > > fullFileName = fullfile(folder, baseFileName);
> > > > xlswrite(fullFileName);
>
> > > > Regards,
> > > > ImageAnalyst
>
> > > thank you ImageAnalyst,
> > > your example seems to be helpful, but i can not understand it. I tried it many times but gave may error also. could you please write a simple one , by which i can make copy and past in command window and it works directly, like
>
> > > ????
> > > for n=1:5,
> > > m=n*pi
> > > end
> > > xlaswrite.......????
>
> > > regards
> > > ragab- Hide quoted text -
>
> > > - Show quoted text -
>
> > ---------------------------------------------------------------------------­---------------------------------------------------------------
> > Hmmmmm....  It's really not that hard.  Nonetheless, I did this code
> > for you:
> > folder = 'C:\Program Files\MATLAB\work';
> > for loopIndex = 1:3
> >    baseFileName = sprintf('Test %d.xls', loopIndex);
> >    if ~exist(folder, 'dir')
> >            mkdir(folder);
> >    end
> >    dataArray = rand(20, 10);
> >    fullFileName = fullfile(folder, baseFileName);
> >    xlswrite(fullFileName, dataArray);
> > end
>
> > It works, though it's very slow.  You'd be better off using xlswrite1
> > () from the File Exchange so that you don't have to open and close
> > Excel at every loop iteration.
> > Good luck,
> > ImageAnalyst
> > P.S. Above you put the xlswrite outside the loop but in your earlier
> > posting you had it inside the loop, so I'm not sure what's up with
> > that.
>
> Hello ImageAnalyst, your code is working, thank you.
> but it gives me three exile files.
>
> When i made the attached code, it gave me 50 files, each one has its iteration value. so, i need all the results to be in a unique file also in a unique sheet.
> May you have a suggestion.
>
> folder ='D:\work';
> for n=1:50;
>     File=sprintf('Test%d.xls',n);
>     if~exist(folder,'dir');  mkdir(folder); end
>     m=n*pi;
>     fileName=fullfile(folder,File);
>     xlswrite(fileName,m);
> end
>
> Regards
> Ragab- Hide quoted text -
>
> - Show quoted text -

-------------------------------------------------------------------------------------------
Ragab:
Did you look at the documentation for xlswrite()?
The worksheet name is one of the optional arguments.
Just use sprintf() to create whatever name you want, and pass it in.
Regards,
ImageAnalyst

Subject: xlswrite

From: ragab Rabeiy

Date: 16 Oct, 2009 16:25:20

Message: 11 of 15


> Did you look at the documentation for xlswrite()?
> The worksheet name is one of the optional arguments.
> Just use sprintf() to create whatever name you want, and pass it in.
> Regards,
> ImageAnalyst

Hello ImageAnalyst,

I don not ask about the worksheet name, please read my message again.

The following code gave me 50 files, each one has its iteration value. But I need all the results to be in a unique file (in one exile file). also your code gave me 3 files, and i want only one file.
note the follwing code:
 
folder ='D:\work';
for n=1:50;
    File=sprintf('Test%d.xls',n);
    if~exist(folder,'dir'); mkdir(folder); end
    m=n*pi;
    fileName=fullfile(folder,File);
    xlswrite(fileName,m);
end

I hope that it not boring for you.
Ragab

Subject: xlswrite

From: ImageAnalyst

Date: 16 Oct, 2009 16:50:19

Message: 12 of 15

I read your messag again and you said "be in a unique file also in a
unique sheet." So I gave you code to do that. You have unique files
because it's inside the for loop. The only way to have unique sheets
(within a single workbook) is to give them different worksheet names.
However if the worksheets are in different workbooks (files) then you
can of course use the same worksheet name.

But now it appears that perhaps what you really mean is to have a
common Excel file (not separate unique ones), and to put all the
various arrays that you calculate/generate in your loop into one
common file. Your code does that but the problem with it is that it
puts all your arrays m into the same location. I'd recommend
calculating a cell reference each pass through your loop, and passing
that in so at least they all get poked into different cells (row/
columns) in the worksheet and don't just keep overwriting the same
location every time.

Again, this is very slow and you'd be better off using xlswrite1().

Subject: xlswrite

From: Nathan

Date: 16 Oct, 2009 16:58:37

Message: 13 of 15

On Oct 16, 9:25 am, "ragab Rabeiy" <r_rab...@yahoo.com> wrote:
> > Did you look at the documentation for xlswrite()?
> > The worksheet name is one of the optional arguments.
> > Just use sprintf() to create whatever name you want, and pass it in.
> > Regards,
> > ImageAnalyst
>
> Hello ImageAnalyst,
>
> I don not ask about the worksheet name, please read my message again.
>
> The following code gave me 50 files, each one has its iteration value. But I need all the results to be in a unique file (in one exile file). also your code gave me 3 files, and i want only one file.
> note the follwing code:
>
> folder ='D:\work';
> for n=1:50;
>     File=sprintf('Test%d.xls',n);
>     if~exist(folder,'dir'); mkdir(folder); end
>     m=n*pi;
>     fileName=fullfile(folder,File);
>     xlswrite(fileName,m);
> end
>
> I hope that it not boring for you.
> Ragab

how about within your loop you specify the cells you want to be
written to?

xlswrite(filename,m,sprintf('A%d',n))
%will write output to cell An, where n is your counter

Subject: xlswrite

From: Mayur Singh

Date: 16 Oct, 2009 18:56:03

Message: 14 of 15

Hi Nathan,

I'm asuming the following :
1. You have no problem in your code if you stored in the variable "m" all the values of n*pi (for n=1 to 50).

2. You are fine if you wrote all the values of n*pi in the first sheet of an excel file starting from cell "A1"(this assumption is for illustration purposes only and you can play with different options for "woorksheet" and "range" to decide where to write the data in a excel file)

3.) lets say filename and location is 'C:\test.xls'

filename = 'C:\test.xls';
m = (1:50)'*pi;
xlswrite(filename,m,1,'A1');








Nathan <ngreco32@gmail.com> wrote in message <7a1ec1d4-02eb-4504-ba86-a10b62b14976@m3g2000pri.googlegroups.com>...
> On Oct 16, 9:25?am, "ragab Rabeiy" <r_rab...@yahoo.com> wrote:
> > > Did you look at the documentation for xlswrite()?
> > > The worksheet name is one of the optional arguments.
> > > Just use sprintf() to create whatever name you want, and pass it in.
> > > Regards,
> > > ImageAnalyst
> >
> > Hello ImageAnalyst,
> >
> > I don not ask about the worksheet name, please read my message again.
> >
> > The following code gave me 50 files, each one has its iteration value. But I need all the results to be in a unique file (in one exile file). also your code gave me 3 files, and i want only one file.
> > note the follwing code:
> >
> > folder ='D:\work';
> > for n=1:50;
> > ? ? File=sprintf('Test%d.xls',n);
> > ? ? if~exist(folder,'dir'); mkdir(folder); end
> > ? ? m=n*pi;
> > ? ? fileName=fullfile(folder,File);
> > ? ? xlswrite(fileName,m);
> > end
> >
> > I hope that it not boring for you.
> > Ragab
>
> how about within your loop you specify the cells you want to be
> written to?
>
> xlswrite(filename,m,sprintf('A%d',n))
> %will write output to cell An, where n is your counter

Subject: xlswrite

From: Nathan

Date: 16 Oct, 2009 21:00:46

Message: 15 of 15

On Oct 16, 11:56 am, "Mayur Singh" <mayu...@gmail.com> wrote:
> Hi Nathan,
>
> I'm asuming the following :
> 1. You have no problem in your code if you stored in the variable "m" all the values of n*pi (for n=1 to 50).
>
> 2. You are fine if you wrote all the values of n*pi in the first sheet of an excel file starting from cell "A1"(this assumption is for illustration purposes only and you can play with different options for "woorksheet" and "range" to decide where to write the data in a excel file)
>
> 3.) lets say filename and location is 'C:\test.xls'
>
> filename = 'C:\test.xls';
> m = (1:50)'*pi;
> xlswrite(filename,m,1,'A1');
>
> Nathan <ngrec...@gmail.com> wrote in message <7a1ec1d4-02eb-4504-ba86-a10b62b14...@m3g2000pri.googlegroups.com>...
> > On Oct 16, 9:25?am, "ragab Rabeiy" <r_rab...@yahoo.com> wrote:
> > > > Did you look at the documentation for xlswrite()?
> > > > The worksheet name is one of the optional arguments.
> > > > Just use sprintf() to create whatever name you want, and pass it in.
> > > > Regards,
> > > > ImageAnalyst
>
> > > Hello ImageAnalyst,
>
> > > I don not ask about the worksheet name, please read my message again.
>
> > > The following code gave me 50 files, each one has its iteration value. But I need all the results to be in a unique file (in one exile file). also your code gave me 3 files, and i want only one file.
> > > note the follwing code:
>
> > > folder ='D:\work';
> > > for n=1:50;
> > > ? ? File=sprintf('Test%d.xls',n);
> > > ? ? if~exist(folder,'dir'); mkdir(folder); end
> > > ? ? m=n*pi;
> > > ? ? fileName=fullfile(folder,File);
> > > ? ? xlswrite(fileName,m);
> > > end
>
> > > I hope that it not boring for you.
> > > Ragab
>
> > how about within your loop you specify the cells you want to be
> > written to?
>
> > xlswrite(filename,m,sprintf('A%d',n))
> > %will write output to cell An, where n is your counter
>
>

Just a note: Why were you addressing me?

-Nathan

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

Tag Activity for This Thread
Tag Applied By Date/Time
xlswrite ragab Rabeiy 15 Oct, 2009 10:44:06
15102009 ragab Rabeiy 15 Oct, 2009 06:09:10
rssFeed for this Thread
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com