How to convert data set into .dat

3 次查看(过去 30 天)

采纳的回答

Walter Roberson
Walter Roberson 2013-5-8
There is no standard format for ".dat" files. ".dat" files can be in whatever format is convenient, so it is important that whatever program is producing the file and whatever program is using the file negotiate a suitable format.
fid = fopen('dermatology.data', 'rt');
fmt = repmat('%d', 1, 35);
datacell = textscan(fid, fmt, 'delimiter', ',', 'CollectOutput', 1);
fclose(fid);
data_array = datacell{1};
fid = fopen('dermadat.dat', 'w');
fwrite(fid, data_array.', 'uint8'); %for example, write it one byte per entry
fclose(fid);
The above code sample writes one integer byte per entry, and writes the data as it reads "across" (so the second entry in the output file corresponds to the second entry on the first line.) Note: if you leave out the .' in the fwrite() then the data will be written "down" (so the second entry in the output file corresponds to the first entry on the second line.)
  2 个评论
dominix
dominix 2013-5-8
but, why does not all data completed (only 34 row).
Walter Roberson
Walter Roberson 2013-5-8
Ah, I did not notice that there are some '?' in the data.
fid = fopen('dermatology.data', 'rt');
fmt = repmat('%f', 1, 35);
datacell = textscan(fid, fmt, 'delimiter', ',', 'CollectOutput', 1, 'TreatAsEmpty', '?');
fclose(fid);
data_array = datacell{1};
fid = fopen('dermadat.dat', 'w');
fwrite(fid, data_array.', 'single'); %it has NaN. Write it as single or double
fclose(fid);
In order to preserve the '?' as unknown values, the data has to be read in as single or double and has to be stored as single or double. If you want the '?' to be converted to some other value, that could also be done.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Standard File Formats 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by