Borrar filtros
Borrar filtros

call recursive fun. erase txtfile

2 visualizaciones (últimos 30 días)
huda nawaf
huda nawaf el 7 de Sept. de 2012
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

Respuesta aceptada

Jan
Jan el 7 de Sept. de 2012
Editada: Jan el 7 de Sept. de 2012
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 comentarios
huda nawaf
huda nawaf el 13 de Sept. de 2012
thanks i will try
huda nawaf
huda nawaf el 14 de Sept. de 2012
thanks jan, it is working now

Iniciar sesión para comentar.

Más respuestas (1)

Azzi Abdelmalek
Azzi Abdelmalek el 9 de Sept. de 2012
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 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 9 de Sept. de 2012
Editada: Azzi Abdelmalek el 9 de Sept. de 2012
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 el 11 de Sept. de 2012
thanks, do u mean open file in main program?
I will try do that. if I failed , I will send my code.
thanks again

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by