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:
xls file in matlab

Subject: xls file in matlab

From: george veropoulos

Date: 8 Jul, 2013 08:24:09

Message: 1 of 5

Dear friend

i run the following code in matlab


spin=1/2;
r_parallel=10;
theta=0;
n=1;
l=0;
yi=1;
B=ones(size(10000));
Enl=ones(size(10000));
wave_function=ones(size(10000));
for i=0:5
B=i;
[wave_function,Enl]=psi_parallel(r_parallel,theta,n,l,spin,yi,B);

xlswrite('results.xlsx', B,'sheet1','A')
xlswrite('results.xlsx',Enl,'sheet1','B')
end


the problem is that in xls(RESULTS) file i have ONLY one value
for B=5 NOT ALL THE VALUES .(i=0,1,3,5 )

please help me
thank you in advance

Subject: xls file in matlab

From: TideMan

Date: 8 Jul, 2013 09:53:32

Message: 2 of 5

On Monday, July 8, 2013 8:24:09 PM UTC+12, george veropoulos wrote:
> Dear friend
>
>
>
> i run the following code in matlab
>
>
>
>
>
> spin=1/2;
>
> r_parallel=10;
>
> theta=0;
>
> n=1;
>
> l=0;
>
> yi=1;
>
> B=ones(size(10000));
>
> Enl=ones(size(10000));
>
> wave_function=ones(size(10000));
>
> for i=0:5
>
> B=i;
>
> [wave_function,Enl]=psi_parallel(r_parallel,theta,n,l,spin,yi,B);
>
>
>
> xlswrite('results.xlsx', B,'sheet1','A')
>
> xlswrite('results.xlsx',Enl,'sheet1','B')
>
> end
>
>
>
>
>
> the problem is that in xls(RESULTS) file i have ONLY one value
>
> for B=5 NOT ALL THE VALUES .(i=0,1,3,5 )
>
>
>
> please help me
>
> thank you in advance

Yes, because you're printing it in A1 each time through the loop.
But, first, what do you expect to get from this statement:
B=ones(size(10000));
?
Try this:
size(ones(size(10000)))
Is that what you expected?
why not:
B=ones(1000,1);
Then put your calculations inside the loop and write to Excel outside the loop.
 

Subject: xls file in matlab

From: Steven_Lord

Date: 8 Jul, 2013 14:46:27

Message: 3 of 5



"george veropoulos" <veropgr@yahoo.gr> wrote in message
news:krdsv9$m8a$1@newscl01ah.mathworks.com...
> Dear friend
> i run the following code in matlab
>
>
> spin=1/2;
> r_parallel=10;
> theta=0;
> n=1;
> l=0;
> yi=1;
> B=ones(size(10000));

As TideMan pointed out, this creates a 1-by-1 B containing the value 1. But
there's another problem:

> Enl=ones(size(10000));
> wave_function=ones(size(10000));
> for i=0:5

I recommend NOT using either i or j for your loop variable.

http://www.mathworks.com/help/matlab/ref/i.html
http://www.mathworks.com/help/matlab/ref/j.html

> B=i;

This overwrites the entire contents of B with the scalar (1-by-1) value of
the loop variable. Thus even if you made B be 10000-by-1:

B = ones(10000, 1);

This line will make it 1-by-1. To make B be a 10000-by-1matrix each of whose
elements is the loop variable, use one of these two lines in the loop:

B = i*ones(10000, 1);
B = repmat(i, 10000, 1);

> [wave_function,Enl]=psi_parallel(r_parallel,theta,n,l,spin,yi,B);
>
> xlswrite('results.xlsx', B,'sheet1','A')
> xlswrite('results.xlsx',Enl,'sheet1','B')
> end
>
>
> the problem is that in xls(RESULTS) file i have ONLY one value for
> B=5 NOT ALL THE VALUES .(i=0,1,3,5 )

Each call to XLSWRITE overwrites the same elements in the sheet sheet1 of
results.xlsx. Rather than write scalar values to the sheet once per loop,
accumulate the results in an array and write the array to the Excel file
after the loop is complete.


% Yes, I know there's a better way to do this.
% This is for demonstration purposes only
z = zeros(10, 1);
for k = 1:10
    z(k) = k.^2;
end

xlswrite('results_example.xlsx', z, 'sheet1', 'A')

Plus your description skipped over 2 and 4. Those loop iterations do run and
XLSWRITE will be called during those iterations.

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: xls file in matlab

From: george veropoulos

Date: 15 Jul, 2013 08:50:13

Message: 4 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <krejc3$hca$1@newscl01ah.mathworks.com>...
>
>
> "george veropoulos" <veropgr@yahoo.gr> wrote in message
> news:krdsv9$m8a$1@newscl01ah.mathworks.com...
> > Dear friend
> > i run the following code in matlab
> >
> >
> > spin=1/2;
> > r_parallel=10;
> > theta=0;
> > n=1;
> > l=0;
> > yi=1;
> > B=ones(size(10000));
>
> As TideMan pointed out, this creates a 1-by-1 B containing the value 1. But
> there's another problem:
>
> > Enl=ones(size(10000));
> > wave_function=ones(size(10000));
> > for i=0:5
>
> I recommend NOT using either i or j for your loop variable.
>
> http://www.mathworks.com/help/matlab/ref/i.html
> http://www.mathworks.com/help/matlab/ref/j.html
>
> > B=i;
>
> This overwrites the entire contents of B with the scalar (1-by-1) value of
> the loop variable. Thus even if you made B be 10000-by-1:
>
> B = ones(10000, 1);
>
> This line will make it 1-by-1. To make B be a 10000-by-1matrix each of whose
> elements is the loop variable, use one of these two lines in the loop:
>
> B = i*ones(10000, 1);
> B = repmat(i, 10000, 1);
>
> > [wave_function,Enl]=psi_parallel(r_parallel,theta,n,l,spin,yi,B);
> >
> > xlswrite('results.xlsx', B,'sheet1','A')
> > xlswrite('results.xlsx',Enl,'sheet1','B')
> > end
> >
> >
> > the problem is that in xls(RESULTS) file i have ONLY one value for
> > B=5 NOT ALL THE VALUES .(i=0,1,3,5 )
>
> Each call to XLSWRITE overwrites the same elements in the sheet sheet1 of
> results.xlsx. Rather than write scalar values to the sheet once per loop,
> accumulate the results in an array and write the array to the Excel file
> after the loop is complete.
>
>
> % Yes, I know there's a better way to do this.
> % This is for demonstration purposes only
> z = zeros(10, 1);
> for k = 1:10
> z(k) = k.^2;
> end
>
> xlswrite('results_example.xlsx', z, 'sheet1', 'A')
>
> Plus your description skipped over 2 and 4. Those loop iterations do run and
> XLSWRITE will be called during those iterations.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Thank you four your answers

A last question Q
there a matlab code for solving the following
problem

H? ??=? ??

?? is the hamiltonian in a quantum mechanics problem
?? is the eigenfunction
E is the eigenvalue

Subject: xls file in matlab

From: george veropoulos

Date: 15 Jul, 2013 09:07:12

Message: 5 of 5

"Steven_Lord" <slord@mathworks.com> wrote in message <krejc3$hca$1@newscl01ah.mathworks.com>...
>
>
> "george veropoulos" <veropgr@yahoo.gr> wrote in message
> news:krdsv9$m8a$1@newscl01ah.mathworks.com...
> > Dear friend
> > i run the following code in matlab
> >
> >
> > spin=1/2;
> > r_parallel=10;
> > theta=0;
> > n=1;
> > l=0;
> > yi=1;
> > B=ones(size(10000));
>
> As TideMan pointed out, this creates a 1-by-1 B containing the value 1. But
> there's another problem:
>
> > Enl=ones(size(10000));
> > wave_function=ones(size(10000));
> > for i=0:5
>
> I recommend NOT using either i or j for your loop variable.
>
> http://www.mathworks.com/help/matlab/ref/i.html
> http://www.mathworks.com/help/matlab/ref/j.html
>
> > B=i;
>
> This overwrites the entire contents of B with the scalar (1-by-1) value of
> the loop variable. Thus even if you made B be 10000-by-1:
>
> B = ones(10000, 1);
>
> This line will make it 1-by-1. To make B be a 10000-by-1matrix each of whose
> elements is the loop variable, use one of these two lines in the loop:
>
> B = i*ones(10000, 1);
> B = repmat(i, 10000, 1);
>
> > [wave_function,Enl]=psi_parallel(r_parallel,theta,n,l,spin,yi,B);
> >
> > xlswrite('results.xlsx', B,'sheet1','A')
> > xlswrite('results.xlsx',Enl,'sheet1','B')
> > end
> >
> >
> > the problem is that in xls(RESULTS) file i have ONLY one value for
> > B=5 NOT ALL THE VALUES .(i=0,1,3,5 )
>
> Each call to XLSWRITE overwrites the same elements in the sheet sheet1 of
> results.xlsx. Rather than write scalar values to the sheet once per loop,
> accumulate the results in an array and write the array to the Excel file
> after the loop is complete.
>
>
> % Yes, I know there's a better way to do this.
> % This is for demonstration purposes only
> z = zeros(10, 1);
> for k = 1:10
> z(k) = k.^2;
> end
>
> xlswrite('results_example.xlsx', z, 'sheet1', 'A')
>
> Plus your description skipped over 2 and 4. Those loop iterations do run and
> XLSWRITE will be called during those iterations.
>
> --
> Steve Lord
> slord@mathworks.com
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com


Thank you four your answers

A last question Q
there a matlab code for solving the following
problem

H? Qi?=E Q?

?? is the hamiltonian in a quantum mechanics problem
Q? is the eigenfunction(wave function)
E is the eigenvalue

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