Convert a .dat file to a .text or .xlsx file

24 views (last 30 days)
Max
Max on 29 Aug 2013
Answered: Cam Salzberger on 31 Aug 2017
Hi I would like to convert a folder with about 1,000 files all in .dat format to a new format either .text or .xlsx without going into excel and re-saving all 1,000 individual files.
I have tryed just loading the files into matlab in the current .dat format using
1.) fopen
2.) importdata
3.) imread
4.) load
all read the file but they do so as if the data inside the file is just one column when infact the files all have roughly 400 columns of data. I would like to successfully read each individual column into matlab just like it would for example, when importing an excel file using xlsread.
Thank you very much for your help,
- Max
  1 Comment
dpb
dpb on 29 Aug 2013
What's a '.dat' file format? The file extension has no actual bearing on the format. If you just want the extension changed, then
system('ren *.dat *.txt')
will do the trick.

Sign in to comment.

Answers (1)

Cam Salzberger
Cam Salzberger on 31 Aug 2017
Hello Max,
I'm pretty surprised that all of fopen, importdata, imread, and load were able to read some form of data into MATLAB. They are generally used for very different things.
fopen simply opens the file for reading. You would still need to use other low-level file read commands to actually retrieve the data, like fscanf, fgetl, fread, etc.
importdata is a pretty good first function to try. It tries to read the data based on its format, interpreting it as best it can. You may also want to check out the Data Import Tool, which can be useful for both getting the data in, and generating code to allow you to do the same for other similar files.
imread is generally used only for image files. I'm not sure how it tried to interpret your DAT file.
load is generally used for MAT files only, though it can work for any ASCII data files. It will try to interpret the data as well, but there are better options.
As dpb said, it really depends on how your data in the file is structured, and what the data actually is. I am going to make the assumption that the file is some form of text file, contains numeric data, and there is some form of separator between the data values. Open the file up in some notepad-like program (or the MATLAB Editor) to verify this is the case.
Once you determine what the separator is between the data columns, you can provide that to the delimiter argument to importdata, dlmread, readtable, or any similar function.
Note that if the number of columns is different between the different rows, MATLAB may have some difficulty reading the file. readtable may be your best bet in that case.
-Cam

Community Treasure Hunt

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

Start Hunting!