Extract Data from .dat File in MATLAB

  • Context: MATLAB 
  • Thread starter Thread starter ironcross77
  • Start date Start date
  • Tags Tags
    Data Matlab
Click For Summary

Discussion Overview

The discussion revolves around extracting data from a .dat file using MATLAB. Participants explore methods to automate the extraction of specific columns from multiple files, addressing both technical approaches and practical considerations.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Experimental/applied

Main Points Raised

  • One participant requests assistance in writing an .m file to extract three specific columns from a .dat file.
  • Another participant suggests editing the .dat file to retain only the three columns and using the "load" function in MATLAB to create a matrix.
  • A participant emphasizes the impracticality of manual editing due to the large number of files (over 2000) and insists on needing an automated solution.
  • Another participant proposes a UNIX command-line solution to automate the editing of multiple .dat files, filtering out unnecessary lines.
  • One participant recommends using the "fscanf" function to read the first six lines and then read the three columns into a matrix.
  • Another suggestion involves creating save and load functions to emulate the structure of the files for easier extraction of desired information.

Areas of Agreement / Disagreement

Participants express a consensus on the need for an automated solution due to the volume of files, but there are differing opinions on the best method to achieve this, with multiple approaches proposed without a clear agreement on a single solution.

Contextual Notes

Some methods suggested depend on specific environments (e.g., UNIX) and may require additional context about the structure of the .dat files beyond the provided example.

ironcross77
Messages
21
Reaction score
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
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.
 
I have more than 2000 of such files. It cannot be done manually.
I need MATLAB to automatically to do it.
 
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.
 
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.
 
create save and load functions...so that the save will emulate the structure of the files that you have...then create the load function based off the save function and you should eb able to extract all information adn then leave out the ones you want.
 
thanks everybody
 

Similar threads

Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
Replies
7
Views
3K
  • · Replies 1 ·
Replies
1
Views
13K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 4 ·
Replies
4
Views
5K