How to convert data set into .dat

13 views (last 30 days)

Accepted Answer

Walter Roberson
Walter Roberson on 8 May 2013
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 Comments
dominix
dominix on 8 May 2013
but, why does not all data completed (only 34 row).
Walter Roberson
Walter Roberson on 8 May 2013
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.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!