Importing multiple .txt files containting single colums of numerical data
Mostrar comentarios más antiguos
I'm trying to import multiple .txt files, up to 100 at a time. The files are named in a fashion like 'test_1.txt, test_2.txt'... etc. These .txt files simply contain a 1024 long column of integers. I'm hoping that I can get some code such that I can import all of these files at once, and obtain a variable for each imported file, which will have a suitable name, such as 'test_1' 'test_2' etc. The purpose of this is to perform analysis on each of these sets of data, so first, would it be better to import them into one matrix, with each column or row representing a separate test file, or would my suggestion of separate variables be better suited?
I found the following code somewhere online, and it almost gives me what I want.
datafiles = dir('*.txt');
filename = cell(length(datafiles),1);
data = cell(length(datafiles),1);
for k = 1:length(datafiles)
filename{k} = datafiles(k).name;
data{k} = importdata(filename{k});
end
The result is a 100x1 cell, where each element within the cell is a 1024x1 double; I am not sure if this is a good format for me to do further analysis. How can I alter the code to get the best result I am looking for?
Many thanks.
Respuesta aceptada
Más respuestas (1)
Azzi Abdelmalek
el 4 de En. de 2013
Editada: Azzi Abdelmalek
el 4 de En. de 2013
for k=1:100
data{k}=dlmread(sprintf('test_%d.txt',k))
end
4 comentarios
Azzi Abdelmalek
el 4 de En. de 2013
Check if your files are in the same folder then your matlab working folder. type
dir TRIAL*.txt
Ross
el 4 de En. de 2013
Categorías
Más información sobre Standard File Formats en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!