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

AI Thread Summary
The discussion revolves around processing 1200 .dat files to extract a specific point (x120, y120, z120) from each file and save the results into a new .dat file. The user initially seeks guidance on how to accomplish this using Fortran or MATLAB. Suggestions include using AWK for its simplicity in handling such tasks. The user shares a MATLAB script but encounters an error related to file identification, likely due to whitespace in the filenames (e.g., "data_Ez 0001.dat"). Participants recommend removing whitespace from filenames, using commands like 'ren' or 'mv', or trying the short name format in Windows. They also suggest verifying the format in the sprintf function and using 'dir' to list files correctly. Additional resources from MATLAB Central are provided for troubleshooting the textscan error.
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: 444
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:
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
16
Views
4K
Replies
1
Views
1K
Replies
8
Views
2K
Replies
10
Views
6K
Replies
1
Views
3K
Replies
5
Views
2K
Back
Top