anubhav
anubhav

Reputation: 643

How to store a variable in a file in MATLAB

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?

Upvotes: 0

Views: 1562

Answers (1)

kyokley
kyokley

Reputation: 450

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

Upvotes: 3

Related Questions