Save file to columns

Asked

Viewed 233 times

0

I need to save data from a file .txt to open in excel. I can save the data, so that each loop is saved one below the other, but I can’t save in columns.

The first two columns are the same, which is different are the last two columns.

Function:

for i =   1:9
     vi = vi+(i+10-i);
     misv = (C1.*(1-exp(-C2.*si))-C3.*si).*exp(-C4.*vi);
     misvc = (C1.*(1-exp(-C2.*si))-C3.*si).*mi0.*exp(-C4.*vi);

     TempA = [si; Fsi; misv; misvc];

     FilenameA = strcat(Filename,' - Slip Function','.txt');
     fid = fopen(FilenameA{1},'at+');
     fprintf(fid, 'Slip Slip_Function Slip Slip_Function_Dry Slip_Function_Cont \n');
     fprintf(fid,'------------------\n');
     fprintf(fid,'%.3f %.3f %.3f %.3f\n',TempA);
     fprintf(fid,'------------------\n');
     fclose(fid);

Filing cabinet .txt:

Slip Slip_Function Slip Slip_Function_Dry Slip_Function_Cont 
------------------
0,000 0,000 0,000 0,000;
0,010 0,224 0,208 0,187;
0,020 0,399 0,370 0,333;
0,030 0,536 0,497 0,448;
0,040 0,643 0,597 0,537;
0,050 0,727 0,674 0,607;

Slip Slip_Function Slip Slip_Function_Dry Slip_Function_Cont 
------------------
0,000 0,000 0,000 0,000;
0,010 0,224 0,193 0,173;
0,020 0,399 0,344 0,309;
0,030 0,536 0,461 0,415;
0,040 0,643 0,554 0,498;
0,050 0,727 0,626 0,563;

1 answer

0

If I understood what you want, you want to have only the two initial columns and then have only the data you generated in columns. To do this, one way is to create the data in just one matrix and save this matrix.

Example:

iii=9;
%initialize variables
tempALL=zeros(10,iii*2+2);
tempALL(:,1)=(1:10)'; %primeira coluna
tempALL(:,2)=(2:2:20)'; %segunda coluna

%escreva elas na formato que você que ter a saida.
for a=1:iii;
    tempALL(:,a*2+1)=tempALL(:,1)*a; % aqui são os dados da terceira coluna
    tempALL(:,a*2+2)=tempALL(:,2)*a; % quarta coluna.
    %perceba o aumento no indice da matrix que está sendo salvo os dados
end
%veja que salvo fora do loop, acessos a arquivos em loop em geral não é uma boa pratica.
dlmwrite('myFile.txt',tempALL,'delimiter','\t','precision',3)

see the options of dlmwrite.

Looking at my exit permit, the names don’t come up. A solution is you can create the file the way you are creating, but with names sorted in only one row. Save only the names and close the file. Use the dlmwrite with the option -append and you will have the names and the data. I can put a code for this another time.

If not this, leave a comment I can try to improve.

  • Hello Guto, Thanks for the help, I went through the routine you sent and she is saving a single line. the first two lines are two parameters that are used to calculate the others, as I could not save all parameters in the same file each time the loop runs it saves the two columns again but they only need to be saved in the first loop.

  • @Julianocardoso, I did not understand your comment. Did it work or not? Your initial problem was columns, and the file of my way saves the amount I understood you wanted from columns: 2 initial plus the results.

  • Deculpe Guto, it really worked, I was wrong to put the parameters of my function in his routine.. Thank you very much.....

  • Guto let me ask you another question, when I use dlmwrite the result of the file is this way 1 2 1 2 2 4 3, i.e., all results are written in a row, using the fprintf(fid,'%.5f %.5f %.5f n',Temp_as_dt); it writes three columns but it fills the data row by row, ie it uses all data from column 1 of the matrix Temp_as_dt to fill the 1 rows of the 3 columns then goes to the second row until complete the three columns until finishing the data of the first column of the Matrix Temp_as_dt ai starts with the data of the second column.

  • @Julianocardoso Since you are a novice user, it is worth the recommendation. A comment - in general - is not the place for new questions. A comment such as the first ones are relevant, as they can change/clarify the question/answer. The latter does not require further explanations not necessarily relevant to your initial question. One option is to modify the question, but in this case you would use the answer as the question, which completely changes the question/answer focus. I recommend a new question with an example, and put a link to this question to have the context.

  • @Julianocardoso If you ask another question, put the link here because then I can take a look tb (but no promises to answer).

Show 1 more comment

Browser other questions tagged

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