Save txt file in Matlab

2

Good,

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

I have used in each function something of the type:

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);

It has saved in the file file.txt , but it has saved one line of information in front of the other, which makes it very difficult to read.

The \n command works only in the matlab environment not when saving the file.

How could I save in column format?

    
asked by anonymous 18.05.2018 / 16:30

2 answers

1

I like to use the function dlmwrite

This function has attribute options that can help you define how the data is saved in the file, you can determine delimiter, numeric precision, skip line, etc. An example usage would be:

dlmwrite(nomedoseuarquivo, seusdados, 'delimiter', '\t', 'precision', '%.6f','newline','pc');
    
20.05.2018 / 00:46
0

This problem is a classic of the windows and unix problems.

In addition to the response from ederwander , an option I usually use is function save , I do not know if it does appendix, but in the manual it should inform you. It saves the correct way on your system.

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

I put all the 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 column format variable. The output in all cases is in column format.

    
21.05.2018 / 15:30