call recursive fun. erase txtfile
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Jan
2012-9-7
编辑:Jan
2012-9-7
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
更多回答(1 个)
Azzi Abdelmalek
2012-9-9
maby your fclose(fid) is in the loop
fid = fopen('filename.txt', 'w');
for k=1:10
v=(1:k), %example
fprintf(fid, '%f\t',v);
fprintf(fid, '\n',[]);
end
fclose(fid);
5 个评论
Azzi Abdelmalek
2012-9-9
编辑:Azzi Abdelmalek
2012-9-9
I think you can add this code inside your function just after cluster1
fprintf(fid, '%f\t',cluster1);
fprintf(fid, '\n',[]);
and call your function
fid = fopen('filename.txt', 'w');
F=devide();
fclose(fid);
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 JSON Format 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!