Extract Data from .dat File in MATLAB

  • MATLAB
  • Thread starter ironcross77
  • Start date
  • Tags
    Data Matlab
In summary, the conversation discusses the process of extracting data from a .dat file using MATLAB. The file contains an array with three columns and the goal is to extract these columns automatically. Various solutions are suggested, including editing the file and using MATLAB functions such as "load" and "fscanf". The conversation also mentions the possibility of creating save and load functions to automate the process.
  • #1
ironcross77
21
0
Ok I have a .dat file containing these datas:


_________________________________________________________________
Date and Time : 120106 12:31:32 12:31:39
Location: 1509123
Bearing: 135
Angle : 30

Scan No. Coordinates PhotonCount
255 12 1
255 45 32
255 34 1231
255 21 54321
255 24 1231

______________________________________________________________


So, you can see that the file contains an array containing three coloums.

I want to extract these three coloums into an array using MATLAB

Can someone write an .m file that will do the job ?

Thank You
 
Physics news on Phys.org
  • #2
Just edit the text file so that it contains only the 3 columns of data and nothing else and then give this file some name, say tmp.dat. Then just load it into MATLAB using "load tmp.dat".

This will create a matrix called "tmp" in MATLAB that contains all three columns of data. Then extract each column to a vector using "x=tmp(:,1)", "y=tmp(:,2)" etc.
 
  • #3
I have more than 2000 of such files. It cannot be done manually.
I need MATLAB to automatically to do it.
 
  • #4
ironcross77 said:
I have more than 2000 of such files. It cannot be done manually.
I need MATLAB to automatically to do it.

FWIW - if you are working in any flavor of UNIX the text editing capabilities can do what you want in:
Code:
for file in `ls /path/to/files/*.dat`
do
       sfile=`basename $file`
       grep -v -e Date -e Scan -e Loca -e Bear $file > /new/path/$file
done

All that's left = columns of numbers... in 2000 new files. The old ones are not changed.
 
  • #5
Just use fscanf; intitally call it once for each of the first 6 lines, then use it once more to read the 3 columns into a matrix.
 
  • #6
create save and load functions...so that the save will emulate the structure of the files that you have...then create teh load function based off the save function and you should eb able to extract all information adn then leave out the ones you want.
 
  • #7
thanks everybody
 

Related to Extract Data from .dat File in MATLAB

1. How can I read a .dat file in MATLAB?

To read a .dat file in MATLAB, you can use the load() function. Simply specify the file name and extension as the input, and assign the output to a variable. This will load the data from the .dat file into your MATLAB workspace.

2. Can I extract specific data from a .dat file in MATLAB?

Yes, you can extract specific data from a .dat file in MATLAB using indexing. Once you have loaded the file into your workspace, you can use indexing to access specific rows, columns, or elements of the data. You can also use logical indexing to extract data that meets certain conditions.

3. How do I handle missing data in a .dat file in MATLAB?

To handle missing data in a .dat file, you can use the NaN (Not a Number) value in MATLAB. When you load the file, you can specify the NaN value as the placeholder for any missing data. This will allow you to still perform calculations and analyses on the data, but you will need to handle the NaN values appropriately.

4. How can I save my extracted data from a .dat file in MATLAB?

You can save your extracted data from a .dat file in MATLAB using the save() function. This function allows you to specify the data to be saved, the file name and extension, and any additional options such as compression or file format. This will create a new .mat file with your extracted data.

5. Are there any built-in functions in MATLAB specifically for reading .dat files?

Yes, there is a built-in function in MATLAB called importdata() that is specifically designed for reading .dat files. This function allows you to specify the file name and extension, and it will automatically detect the data format and load the file into your workspace. However, this function may not work for all types of .dat files, so it is always best to check the documentation and test it with your specific file before relying on it.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • Advanced Physics Homework Help
Replies
11
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
12K
  • Programming and Computer Science
Replies
2
Views
687
Back
Top