Importing multiple .txt files containting single colums of numerical data
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
Davide Ferraro
2013년 1월 4일
Hi Ross,
it depends on the type of analyses you need to do. If you are not expert working directly with cell array using command such as CELLFUN you may like to transform this variable into a standard matrix. By using the command CELL2MAT:
data_matrix = cell2mat(data);
you should be able to get a single matrix. In order to get the matrix with each variable in a column a transposition may be needed in your case:
data_matrix = cell2mat(data');
At this point you can use statistical operator directly on the matrix. In example:
mean_values = mean(data_matrix);
will compute the mean of each individual column. This makes your code faster, shorter and simpler. I would avoid getting 100 different variables, you will get crazy in automatically process all of them with complex loops and EVAL instructions.
댓글 수: 4
추가 답변 (1개)
Azzi Abdelmalek
2013년 1월 4일
편집: Azzi Abdelmalek
2013년 1월 4일
for k=1:100
data{k}=dlmread(sprintf('test_%d.txt',k))
end
댓글 수: 4
참고 항목
카테고리
Help Center 및 File Exchange에서 Large Files and Big Data에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!