MATLAB: Call recursive fun. erase txtfile

recursive

hi, I i have recursive function , each time call this function return different array.
I want to print these arrays into txt file, it is important to me store all arrays. But the problem each time is called function will erase the txtfile.
What I have to do to save all arrays?
THANKS IN ADVANCCE

Best Answer

Main function, which opens and closes the file:
fid = fopen(FileName, 'w');
if fid == -1, error('Cannot open file'); end
Recursive(fid, 17);
fclose(fid);
And the recursice function:
function Recursive(fid, Num)
Num = Num - 1;
if Num == 0
return;
end
fprintf(fid, '%d\n', Num);
Recursive(Num);
end
Or you open the file in the recursive function for appending:
function Recursive(Num)
fid = fopen(FileName, 'a');
if fid == -1, error('Cannot open file'); end
fprintf(fid, '%d\n', Num);
fclose(fid);
Num = Num - 1;
if Num > 0
Recursive(Num);
end