Read file from multiples .dat files and choose only one point

In summary, to select a single point from all 1200 .dat files, use importfile and startRow and endRow to specify the offset and length of the file, respectively.
  • #1
s_hy
61
0
Hi all.

I have 1200 .dat files containing x,y,z . What I need to do is to open all the files and choose only one single point from all the files, in example point (x120,y120,z120) from all 1200 .dat file and save it into new .dat file. Can anyone teach me how to do that either in fortran or matlab



thank you
 

Attachments

  • data ez.png
    data ez.png
    2.5 KB · Views: 395
Technology news on Phys.org
  • #2
s_hy said:
Hi all.

I have 1200 .dat files containing x,y,z . What I need to do is to open all the files and choose only one single point from all the files, in example point (x120,y120,z120) from all 1200 .dat file and save it into new .dat file. Can anyone teach me how to do that either in fortran or matlab



thank you

Why not use AWK or some other scripting language?

In awk, this would be particularly easy pretty much a one-liner.
 
  • #3
actually, I did wrote code in matlab

Code:
numFiles = 822;
myData = cell(1,numFiles);
startRow = 6886;
endRow = 6886;

for fileNum = 1:numFiles
    fileName = sprintf('*.dat',fileNum);
    myData{fileNum} = importfile(fileName,startRow,endRow);
end

%% Format string for each line of text:
formatSpec = '%*24s%12f%[^\n\r]';

%% Open the text file.
fileID = fopen(filename,'r');

% This call is based on the structure of the file used to generate this
% code. If an error occurs for a different file, try regenerating the code
% from the Import Tool.
dataArray = textscan(fileID, formatSpec, endRow-startRow+1, 'Delimiter', '', 'WhiteSpace', '', 'HeaderLines', startRow-1, 'ReturnOnError', false);

%% Close the text file.
fclose(fileID);

%% Allocate imported array to column variable names
VarName3 = dataArray{:, 1};

but I got this error

Error using textscan
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in importfile (line 34)
dataArray = textscan(fileID, formatSpec, endRow(1)-startRow(1)+1, 'Delimiter', '', 'WhiteSpace',
'', 'HeaderLines', startRow(1)-1, 'ReturnOnError', false);

Error in readdata (line 18)
myData{fileNum} = importfile(fileName,startRow,endRow);

and I am actually don't understand the reason of this error. Only one in my mind is the filename of datafile is data_Ez 0001.dat and so on. Is it the white space in filename maybe the reason? If yes, how I can remove all the whitespace in all 822 datafile?

thank you
 
  • #4
The whitespace is most likely the culprit. Try an experiment and change the names removing the space and see what happens.

This feature of windows has caused many problems over the years. The most common case is that you create a bat file where you pass in the filename with the space and script in turn interprets it as two arguments not one.

An alternative if this is windows is to try the short name of the file ie file 'xxx yyy zzz.dat' would be xxx~1.dat or something like that. You can use the dir command to find our what it actually is.

You could replace it with an underscore using the 'ren' or 'mv' commands depending on what OS you're working on.

Also MATLAB may have a solution here:

http://www.mathworks.com/matlabcent...use-fopen-to-generate-a-valid-file-identifier

or here if its the spaces issue:

http://www.mathworks.com/matlabcent...s-with-load-when-path-or-filename-has-space-s
 
  • #5
The format in sprintf is wrong. As the error message says, you are trying to open a file with an invalid file name. Remove the semicolon from the end of that line and work it till the printed fileNames are what you want. You might as well skip or comment out other lines till you get your file names right.

Try using fileList = dir( '*.dat') to get the list of files in the directory.
 
Last edited:

1. How do I read multiple .dat files in a scientific dataset?

To read multiple .dat files in a scientific dataset, you can use a programming language such as Python or R to loop through each file and extract the necessary data. Alternatively, you can use a specialized software tool for scientific data analysis that has the capability to read multiple .dat files at once.

2. Can I choose only one point from the multiple .dat files?

Yes, you can choose only one point from the multiple .dat files by using indexing or filtering techniques in your programming language or software tool. This will allow you to extract the specific data point that you need from the files.

3. How do I know which point to choose from the .dat files?

The point you choose will depend on your research question and the specific data you are interested in analyzing. You can use visualizations or statistical analysis to determine which point is most relevant to your research. You may also consult with a data expert or domain specialist for guidance.

4. Is it possible to merge the data from the multiple .dat files into one file?

Yes, it is possible to merge the data from multiple .dat files into one file. This can be done using programming languages or specialized software tools that have the capability to combine data from different sources. However, it is important to ensure that the data is compatible and can be merged accurately.

5. How can I ensure the accuracy of the data when choosing only one point from the .dat files?

To ensure accuracy when choosing one point from the .dat files, it is important to carefully review and validate the data. This can involve checking for any missing or erroneous values, performing statistical analyses, and consulting with experts in the field. It is also helpful to document your data selection process and any assumptions made in order to ensure transparency and reproducibility.

Similar threads

  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
19
Views
1K
  • Programming and Computer Science
Replies
8
Views
877
  • Programming and Computer Science
2
Replies
65
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
3K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
10
Views
6K
Back
Top