Building an array of values from multiple .txt files
2 次查看(过去 30 天)
显示 更早的评论
Hi, I have several hundreds of files which I need to read, take out 3 columns of data (numbers) and then create a cell of the data so that I can use it within the workspace.
For each of the 3 columns, the data of each file need to be added to the end of the previous column.
So far I have created and ordered an array of the file names to be read and used fopen to open each file in the correct order within a loop. I then use textscan to build a cell array of the data. My problem is actually adding new values to this cell. I have tried using ‘cat’ to concatenate the previous and new cells but this just adds new cells within my C_abxyz cell.
Here is my code so far for the loop:
for i = 1:length(files)
file = char(files(i));
fid = fopen(file);
C_abxyz{:,i} = textscan(fid,'%f%f%f','Delimiter',',','Headerlines',1);
fclose(fid);
if i>1
C_abxyz{i} = cat(1,C_abxyz{i-1}, C_abxyz{i});
% C_abxyz{i} = [C_abxyz{i-1} C_abxyz{i}];
% XYZ = [C_abxyz{i-1};C_abxyz{i}];
end
end
This builds C_abxyz as a 1x154 cell (as there are 154 files). Te cells within C_abxyz then start at a 1x3 cell, then a 2x3 cell, 3x3 cell up to a 154x 3 cell due to the re-definition of C_abxyz at each loop iteration. The 154x3 cell almost does what I need, in the sense that the values of the columns for each file are added below their previous respective columns. But these are all within cells. What I require is 3, 15554x1 cells (101*154 = 15554), containing all of the column data to be created inside the 1x3 C_abxyz cell.
I hope that makes sense. I would very much appreciate any help!
Thanks,
Earle
0 个评论
采纳的回答
Oleg Komarov
2011-7-5
EDIT
% Use vertical allocation and CollectOutput:
C_abxyz(i,:) = textscan(fid,'%f%f%f','Delimiter',',','Headerlines',1,'CollectOutput',1); % Note round brackets
% Then to consolidate all of the data (outside of the loop):
C_abxyz = cat(1,C_abxyz{:});
更多回答(1 个)
Bob Hamans
2011-7-5
Hi Earle, something like this should work:
C_abxyz = {}; % Initialize variable
for i = 1:length(files) % Loop
file = char(files(i));
fid = fopen(file);
C_temp = textscan(fid,'%f%f%f','Delimiter',',','Headerlines',1);
C_abxyz = cat(1,C_abxyz,C_temp); % C_abxyz, growing in loop
fclose(fid);
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!