xlswrite

3 views (last 30 days)
Nasir Qazi
Nasir Qazi on 23 Mar 2012
I have a Program where a values of 'A' is changing in each iteration and I want to save each value separately in excel so later I can make a graph, I am using xlswrite but it overwrite the previous value which I don't want them to change , how can I do that ..

Accepted Answer

Ken Atwell
Ken Atwell on 23 Mar 2012
Hi Nasir,
Is the value of A a scalar? Even if you could make this work, writing to an Excel file at every loop iteration sounds like slow going. How about preserving the values of A within MATLAB itself:
allA = zeros(numLoopIterations, 1);
for i=1:numLoopIterations
A = ...
allA(i) = A;
end
plot(allA)
I've pre-allocated above for performance reasons, but I suspect even not pre-allocating (if numLoopIternations is not known, for example) will be much faster than writing to Excel:
allA = [];
while ...
A = ...
allA(end+1) = A;
end
plot(allA)
  2 Comments
Cynthia
Cynthia on 4 Apr 2012
This answer was very helpful, but how do you do this if A is a vector, not a scalar? I'm not plotting the data but I'm exporting it into MS Excel. The last iteration keeps getting written over just like Nasir's plot was written over.
Ken Atwell
Ken Atwell on 4 Apr 2012
In the vector case, if A is always the same length, it is only incrementally more complicated. You can create a 2-D matrix, with each row representing one "column" of A:
allA = zeros(numLoopIterations, lengthOfVectorfA);
for i=1:numLoopIterations
A = ...
allA(i,:) = A;
end
If A is not the same length in each iteration (i.e., your matrix/spreadsheet will be ragged right), you will need to use a cell array or some such to record vectors of varying lengths. Is this the case for you, Cynthia?

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!