Save txt file to Matlab

Asked

Viewed 3,737 times

2

Good,

I have a program that runs in loop and I need to save the results of each loop. Within the loop i call several functions and in each function I need to save the results.

I have used in every function something of the kind:

fid = fopen('file.txt','a+');
fprintf(fid,'CoF Inícial:  %5.5f \n',mib);
fprintf(fid,'CoF Final:  %2.5f \n',min(miBPT));
fclose(fid);

he has saved in the archive file.txt, but has saved online one piece of information in front of the other which makes it very difficult to read.

the command \n works only in the environment of Matlab not when saving the file.

How could I save in column format?

2 answers

1

I like to use the function dlmwrite

This function has attribute options that can help you define how data is saved in the file, you can determine delimiter, numerical accuracy, skip line, etc, an example of use would be:

dlmwrite(nomedoseuarquivo, seusdados, 'delimiter', '\t', 'precision', '%.6f','newline','pc');

0

This problem is a classic problem windows vs Unix.

In addition to the response of ederwander, an option I normally use is the save function, I don’t know if she does Appendix, but in the manual she should inform you. She saved it in the correct way in your system.

However, if you want to use your code, just modify your code, either through as you opens the file or how you write.

I put all options below.

save('myfilename.dat','myvar','-ascii')


% the modifier 't' solves this issue
f=fopen('textfile.dat','at');
fprintf(f,'%4.2f \n',myvar);
fclose(f);


%the "enter" is a linefeed(\n) + carriagereturn(\r)
f=fopen('textfile.dat','a');
fprintf(f,'%4.2f \r\n',myvar);
fclose(f);

In this case, myvar is a variable in column format. The output in all cases is in column format.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.