How can the contents of data files be stored as an array of strings in matlab?

1 次查看(过去 30 天)
I have a Db (.dat) files that need to be compared inside my code. So I have to store them in some variable first.
g=fileread ('e:\ bla..'); // to determine the size of the files
f=['e:' filesep 'dfiles'];
a=dir(f); //a(i) are the files that are to be opened in the function
data=zeros(size(g),15); //assuming 15 files are to be opened
for i=1:5
data(i)=importdata(fullfile(f,a(i)));
end

采纳的回答

Ken Atwell
Ken Atwell 2012-4-10
Thanks for providing sample data. The import tool should be able to handle this in recent versions of MATLAB. In R2012a:
  1. In the Current Folder window, navigate to the folder containing the file.
  2. Right-click -> Import Data
  3. Click "Next', then "Finish"
If you need a programmatic way to do this, check "Generate MATLAB code" in step 3 and examine the generated source code. The MATLAB function importdata will be able handle this file with little fuss.
  3 个评论

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2012-4-10
There is no standard format for .dat files, so we cannot answer the question except in generalities.
The ".dat" file extension refers to any data file in any format. Any one program might be able to understand a particular .dat file, but the next program over might use a completely different .dat file format. There are too many programs in the world to be able to allocate a different 3-letter extension for each individual data file format.
If you do not know the format of the .dat files then you have to hope that you can either figure it out yourself by examining the files, or else that you can figure out the format by examining the documentation of the program that created the file.
Reading a data file without knowing its format is like doing a large crossword puzzle without any of the clues -- and getting every letter completely right.
  3 个评论
Ken Atwell
Ken Atwell 2012-4-10
Dee, ".dat" is a generic file extension that can refer to just about anything. Do you know if the file is text or binary? If it is binary, you really need to consult with the producer of the .dat file to have a reasonable chance at decoding it. If it is text, you are in a far better position to parse the file in MATLAB.
If you don't know if the .dat file is text or binary, try this: Open the .dat file in the MATLAB editor. Do this by using the Current Folder window to navigation to the file in question; then right-click -> "Open as Text". What do you see? Is the file readable to your human eyes? If so, great, post the first 10-20 lines of the file here and maybe someone can help with the parsing. If you mostly see garage (seemingly random, funny-looking glyphs with maybe small snippets of recognizable text), the file is binary and you have a tougher road ahead of you.
dee koshy
dee koshy 2012-4-10
it is a text file. basically contains different x,y positions and some orientation data from an image file.
2.8700000e+002 1.1800000e+002 -9.3804749e-001
1.3400000e+002 7.5000000e+001 2.3424967e+000
9.4000000e+001 1.7000000e+001 -7.7416268e-001
9.4000000e+001 2.0700000e+002 -2.1432561e+000
1.0600000e+002 1.7000000e+001 -8.7160374e-001
1.0600000e+002 2.0700000e+002 -2.1432561e+000
1.3300000e+002 1.7000000e+001 -1.1160771e+000
8.1000000e+001 7.8000000e+001 2.6344941e+000
1.7100000e+002 7.7000000e+001 1.9320001e+000
2.9700000e+002 1.7000000e+001 -1.0222469e+000
3.0900000e+002 7.4000000e+001 -8.5570463e-001
1.4400000e+002 1.3400000e+002 -2.9018495e+000
2.1200000e+002 9.3000000e+001 -1.2490458e+000
4.0000000e+001 1.7400000e+002 -2.7803889e+000
3.1000000e+001 1.7400000e+002 -2.7803889e+000
1.0500000e+002 1.7200000e+002 8.2335461e-001
1.2400000e+002 1.9300000e+002 1.1341692e+000
about 500 lines of this per file.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Data Import and Export 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by