call recursive fun. erase txtfile

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

 채택된 답변

Jan
Jan 2012년 9월 7일
편집: Jan 2012년 9월 7일

0 개 추천

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

댓글 수: 4

huda nawaf
huda nawaf 2012년 9월 9일
편집: huda nawaf 2012년 9월 9일
thanks, jan, each time call function will return vector with different length.
i tried to use structure as output of function , but i failed.
i need save this vector .is it best to save inside function or in main program.
i think your code will also erase what wrote in previose call. i need a way by which I do not lost the previose return.
thanks
Jan
Jan 2012년 9월 10일
You think that the code will erase the formerly written data? I suggest to try it instead. fopen(FileName, 'a') opens a file for appending...
huda nawaf
huda nawaf 2012년 9월 13일
thanks i will try
huda nawaf
huda nawaf 2012년 9월 14일
thanks jan, it is working now

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 9일

0 개 추천

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

huda nawaf
huda nawaf 2012년 9월 9일
thanks, but i used recursive function. i want each time call this fun. do not loss the previose return . how can store the return values?
this is my problem
I used recursive function F=devide() when run this function , the output:
cluster1 =
1 2 4 5
3 6 7 0
1 2 0 0
3 4 0 0
1 0 0 0
2 0 0 0
cluster1 =
1 2 4 5
3 6 7 0
1 2 0 0
3 4 0 0
1 0 0 0
2 0 0 0
cluster1 =
1 2 4 5
3 6 7 0
1 2 0 0
3 4 0 0
cluster1 =
1 2 4 5
3 6 7 0
F =
1 2 4 5
3 6 7 0
the problem in F , where F it must has the first array in output (6*4) and this array that must returned to main functon . But , in fact F has the last output the array 2*4. I can not control on output to make the last processing of array is what return to main program.
thanks
Azzi Abdelmalek
Azzi Abdelmalek 2012년 9월 9일
if you post your function, it will be helpfull
Azzi Abdelmalek
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);
huda nawaf
huda nawaf 2012년 9월 11일
thanks, do u mean open file in main program?
I will try do that. if I failed , I will send my code.
thanks again

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

질문:

2012년 9월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by