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

Click For Summary

Discussion Overview

The discussion revolves around the challenge of reading multiple .dat files to extract a specific data point (x120, y120, z120) and saving it into a new .dat file. Participants are exploring solutions primarily in MATLAB, with some suggestions for alternative scripting languages.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant requests assistance in reading 1200 .dat files to extract a specific point and save it, asking for help in either Fortran or MATLAB.
  • Another participant suggests using AWK or a similar scripting language, noting that it could be accomplished with a simple one-liner.
  • A participant shares MATLAB code they wrote to read the files but encounters an error related to an invalid file identifier when using the textscan function.
  • Concerns are raised about whitespace in filenames potentially causing issues, with a suggestion to rename files to remove spaces.
  • Another participant points out that the format in the sprintf function is incorrect and advises checking the generated filenames to ensure they are valid.
  • Suggestions are made to use the dir command in MATLAB to list the .dat files in the directory as a way to troubleshoot file access issues.

Areas of Agreement / Disagreement

Participants express differing opinions on the best approach to handle the file reading and error resolution, with no consensus reached on a single solution. The discussion remains unresolved regarding the optimal method to extract the desired data point.

Contextual Notes

Limitations include potential issues with file naming conventions, particularly concerning whitespace, and the need for further debugging of the provided MATLAB code. The discussion does not resolve the underlying causes of the errors encountered.

s_hy
Messages
57
Reaction score
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: 465
Technology news on Phys.org
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.
 
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
 
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
 
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:

Similar threads

  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 19 ·
Replies
19
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 10 ·
Replies
10
Views
6K
  • · Replies 1 ·
Replies
1
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
65
Views
5K
  • · Replies 12 ·
Replies
12
Views
2K