Building an array of values from multiple .txt files

5 vues (au cours des 30 derniers jours)
Earle
Earle le 5 Juil 2011
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

Réponse acceptée

Oleg Komarov
Oleg Komarov le 5 Juil 2011
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{:});
  7 commentaires
Oleg Komarov
Oleg Komarov le 5 Juil 2011
@Sean: :P
Earle
Earle le 5 Juil 2011
Its working! I built it as a cell because thats how the existing code works. Thanks for your help!

Connectez-vous pour commenter.

Plus de réponses (1)

Bob Hamans
Bob Hamans le 5 Juil 2011
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
  4 commentaires
Earle
Earle le 5 Juil 2011
Thanks. I've tried that but it's still building C_abxyz as a 154x3 cell. The difference between this version and yur previous suggestion is that the 101x1 double in the leftmost column has been replaced by a 1x3 cell. I feel like its very close, but I'm not sure about quite how to get it to work!
Earle
Earle le 5 Juil 2011
Sure, is this ok? -
21973.7805241942, 121371.107863843, -16359.999999422
21973.7707782736, 121371.163139238, -16385.482754882
21973.7621599047, 121371.212019561, -16410.9655238176
21973.7546690887, 121371.25450481, -16436.448304574
21973.7483058265, 121371.290594981, -16461.9310954964

Connectez-vous pour commenter.

Catégories

En savoir plus sur Text Data Preparation dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by