Comma separated data with text using dlmwrite
Mostrar comentarios más antiguos
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
Más respuestas (1)
Sausan Khomusi
el 4 de Ag. de 2013
0 votos
5 comentarios
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
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":
- 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.
- Escape characters coding special characters, e.g. carriage return and new line. Example '\ ' for a new line and a tab.
- 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
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) ;
Categorías
Más información sobre Text Files en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!