How can the contents of data files be stored as an array of strings in matlab?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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
2 comentarios
Jan
el 10 de Abr. de 2012
Please specify "store them in sime variables". Otherwise it is impossible to give any meaningful suggestion.
Respuesta aceptada
Ken Atwell
el 10 de Abr. de 2012
Thanks for providing sample data. The import tool should be able to handle this in recent versions of MATLAB. In R2012a:
- In the Current Folder window, navigate to the folder containing the file.
- Right-click -> Import Data
- 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 comentarios
Walter Roberson
el 10 de Abr. de 2012
The result of dir() is a structure array.
importdata(fullfile(f,a(i).name))
Más respuestas (1)
Walter Roberson
el 10 de Abr. de 2012
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 comentarios
Ken Atwell
el 10 de Abr. de 2012
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.
Ver también
Categorías
Más información sobre Data Import and Export en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!