質問

I have the following text file, THERM2.DAT which has a unique format:

Ag+               g10/97AG 1.E -1.   0.   0.G   298.150  6000.000 1000.        1
 9.72687035E+00-4.01472180E-03 7.47796464E-07-1.76595533E-11-4.14279861E-15    2
 1.17714726E+05-3.91847888E+01 1.67072904E-01 2.30034783E-02-8.05675031E-05    3
 1.14647031E-07-5.08119362E-11 1.22365909E+05 1.49717871E+01 1.22928919E+05    4
Ag-               g10/97AG 1.E  1.   0.   0.G   298.150  6000.000 1000.        1
 2.50067694E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00 0.00000000E+00    2
 1.76654697E+04 5.86586664E+00 2.50067694E+00 0.00000000E+00 0.00000000E+00    3
 0.00000000E+00 0.00000000E+00 1.76654697E+04 5.86586664E+00 1.84110465E+04    4
AIR               L 9/95  WARNING!         0G   200.000  6000.000 1000.        1
 3.08792717E+00 1.24597184E-03-4.23718945E-07 6.74774789E-11-3.97076972E-15    2
-9.95262755E+02 5.95960930E+00 3.56839620E+00-6.78729429E-04 1.55371476E-06    3
-3.29937060E-12-4.66395387E-13-1.06234659E+03 3.71582965E+00-1.50965000E+01    4

This is only a portion of the file, there are hundreds of lines where each group of 4 lines correspond to a particular species.

My goal is to convert all of this data into something more generic, like a CSV file so it can be easily read by various programs like Excel, Matlab, etc. Something like the following would be nice:

Ag+,298,6000,1000,9.72687035E+00,-4.01472180E-03,7.47796464E-07,etc...
Air,200,6000,1000,3.08792717E+00,1.24597184E-03,-4.23718945E-07,etc...

Since I have Matlab, I was wondering if I could use it to export the data to a CSV file. So far I have the following Matlab script:

fid = fopen('THERM2.DAT');
data = textscan(fid,'%s');

Using textscan puts all the data in a cell array but I don't know how to grab the data from the array and write it to CSV formatted text.

役に立ちましたか?

解決

i checked the help documents, here's an example, maybe it's useful

the following string includes a form feed character, \f:

**lyric = sprintf('Blackbird\fsinging\fin\fthe\fdead\fof\fnight');**

To read the string using textscan, call the sprintf function to explicitly convert the form feed:

 **C = textscan(lyric, '%s', 'delimiter', sprintf('\f'));**

textscan returns a 1-by-1 cell array C:

C{1} = {'Blackbird'; 'singing'; 'in'; 'the'; 'dead'; 'of'; 'night'}

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top