views:

208

answers:

1

In order to store a variable dist to a file preferably Excel file, we can use

xlswrite('a.xls', dist)

or

dlmwrite('a.txt', dist, ',')

Problem is suppose the variable dist is in a loop in a program and returns a different value each time the program runs as per the input then each time a.xls is overwritten and I get only the last value of dist written in the file.

How to write all the values of the variable dist to the file?

+3  A: 

Why not just create a temporary array the size of the loop to store the dist values. Then when the loop is finished, just use the xlswrite function to write the array to a file.

For example...

distValues = zeros(loopIter,1);
for i = 0:loopIter

% Make calculations here

distValues(i) = dist;
end

xlswrite('a.xls', distValues);

If a.xls already exists and has values in it that you would like loaded first, try...

inDist = xlsread('a.xls');
distValues = zeros(loopIter,1);
for i =0:loopIter

% Make calculations here

distValues(i) = dist;
end

distValues = [inDist; distValues]

xlswrite('a.xls', distValues);

I hope that helps

Shaka
good idea but if the loop is dependent on whether user wants to run it more or not. eg . dist is calculated in my program at the end and then is a questionbox to user whether he wants to run again or not. If he answers yes then only program runs.??
anubhav
I guess I don't quite understand. So you calculate dist at the end of the program the first time it is run but if the user chooses to run the program again, you don't calculate a new dist?
Shaka
yes i do that again but then that ways loop iterations are not fixed. Anyways i solved it with your idea. I was using a while loop to check whether user wants to run again or not and introduced i in the loop and used it with dist and incremented it each time user selects to run again. Thanks. Can i store a string like 'hello' along with the dist value in the next column and then change to next row for next dist value with again a string and so on.??
anubhav
I'm not actually sure if you can do that with xlswrite... Sounds like you might have to ask a new question
Shaka