Comma separated data with text using dlmwrite

Hello,
I am trying to achieve this string in a .txt file.
SPCD,1,1,3,1.9628e-05
and not this S,P,C,D,1,1,3,1,.,9,6,2,8,e,-,0,5
I have the following data in MATLAB:
n=12;
node_num=linspace(1,n,n)';
A = [1.96276396551253e-05,NaN,1.96298099346691e-05,1.96279640136501e-05;1.96276396551253e-05,NaN,1.96298099346691e-05,1.96279640136501e-05;1.96276396551253e-05,NaN,1.96298099346691e-05,1.96279640136501e-05;]
SPC = reshape(A,n,1);
for j = 1:n
R = {char('SPCD'), num2str(1), num2str(j), num2str(3), num2str(SPC(j))};
dlmwrite('try2.txt', R,'precision', '%.10f','newline', 'pc','-append');
end
I hope this makes sense and you can help. Thank you in advance. Sausan

 Respuesta aceptada

Cedric
Cedric el 4 de Ag. de 2013
Editada: Cedric el 4 de Ag. de 2013
Try with the following instead of DLMWRITE.
fid = fopen('try3.txt', 'w') ; % Opens file for writing.
for j = 1:n
fprintf(fid, 'SPCD,1,%d,3,%.10f\r\n', j, SPC(j)) ;
end
fclose(fid) ; % Closes file.
There is an issue with DLMWRITE which wants to "convert to matrix" its data argument. This is, in your case, what creates this array of comma-separated characters. Also, calling an I/O function which opens/closes a file from within a loop is not efficient; a better solution would be to call it once only with a cell array that contains all the data, which you cannot do because of the issue that I just mentioned.
Note that the format that you are using in the example is not the format (%.10f) specified in the call to DLMWRITE, so you might want to update the call to FPRINTF using a format like %.4e for example.

Más respuestas (1)

Sausan Khomusi
Sausan Khomusi el 4 de Ag. de 2013

0 votos

Thank you very much. This worked perfectly.

5 comentarios

Cedric
Cedric el 4 de Ag. de 2013
You're welcome. I just added a note about the format in my answer, I guess after you read it.
Sausan Khomusi
Sausan Khomusi el 4 de Ag. de 2013
I am trying to apply the same format to write out this output
SPC1,1,3,j ( j = 1:n % same as before)
It is not working for me. I am very unfamiliar with FPRINTF.
If you could help, that would be much appreciated.
Cedric
Cedric el 4 de Ag. de 2013
Editada: Cedric el 4 de Ag. de 2013
FPRINTF takes a first, optional argument that is a file identifier (if not provided, the output goes by default to the command window (to stdout to be more accurate)), a second argument called formatSpec which is a string that defines what to output and how, and a list of variables matching what is defined in the formatSpec.
To understand it better, type
doc fprintf
in the command window, and look for the section about formatSpec. The formatSpec string can contains 3 "things":
  1. Format specifiers starting with %, followed when relevant by operators for specifying e.g. the precision, and ended by a letter which specifies the type of the variable that FPRINTF must convert to string. Example: '%d' for integer variable, '%7.4e' for floating point variable to convert into a string with a field with of 7 and a precision of 4, using exponential notation.
  2. Escape characters coding special characters, e.g. carriage return and new line. Example '\ ' for a new line and a tab.
  3. The rest is "literal", which means outputted as it is (not interpreted).
The formatSpec that I defined above
'SPCD,1,%d,3,%.10f\r\n'
outputs 'SPCD,1,' as it is, then the %d (which is the first format specifier) tells FPRINTF to interpret the first variable that follows, j, as an integer and convert it to string, then ',3,' is literal again, then %.10f (which is the second format specifier) tells FPRINTF to interpret the second variable that follows, SPC(j), as a float with a precision of 10 and convert it to string, and we have two escape characters at the end which output a carriage return \r and a new line \n.
So now you should be able to define the formatSpec that you need. Let me know if you have a problem with that.
Sausan Khomusi
Sausan Khomusi el 4 de Ag. de 2013
Thank you for the fprintf details. I was getting hung up with where the specifiers needed to be, to be applied to the variable.
Your explanation of the formatSpec that you defined helped sort out my thoughts.
If I could pester you still. Once I have reshaped a matrix to a single array, how can I reduce the single array to every 10th value to be in the initial loop.
Cedric
Cedric el 4 de Ag. de 2013
Editada: Cedric el 4 de Ag. de 2013
If I understand well your question, here is an example taking every 6th element:
>> values = 101:120 % Fake data, to illustrate.
values =
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
>> ix = 1:6:length(values) % Indices of relevant elements.
ix =
1 7 13 19
>> values_subset = values(ix)
values_subset =
101 107 113 119
Which you will probably write in a more condensed way once you understand:
>> values_subset = values(1:6:end) ;

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