The Math Forum

Search All of the Math Forum:

Views expressed in these public forums are not endorsed by NCTM or The Math Forum.

Math Forum » Discussions » Software » comp.soft-sys.matlab

Notice: We are no longer accepting new posts, but the forums will continue to be readable.

Topic: xls file in matlab
Replies: 4   Last Post: Jul 15, 2013 5:07 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 18,038
Registered: 12/7/04
Re: xls file in matlab
Posted: Jul 8, 2013 10:46 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

"george veropoulos" <> wrote in message
> 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.

> 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;

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
To contact Technical Support use the Contact Us link on

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© The Math Forum at NCTM 1994-2018. All Rights Reserved.