I have 26 ascii files and I want to import data from all these files with the help of a for loop and convert all of them into column vectors.

I have 26 ascii files and I want to import data from all these files with the help of a for loop and convert all of them into column vectors. I have used the following code but the workspace shows only one of these 26 ascii files converted into a column vector.Need help
%Transfer all the 26 waveforms into a directory files=dir('*.asc');
for k=1:numel(files);
B=importdata(files(k).name);
B=[B.data];
B=B(:);
end
There is no error in the code but I cannot understand why it doesnot show all the 26 column vectors after using the code.I am new to matlab so it would be great if someone can help me with it.
I also need to put all the 26 column vectors into a matrix.
Can anyone please help me correct the script

 Réponse acceptée

F.
F. le 10 Juil 2012
Modifié(e) : F. le 10 Juil 2012
I suppose all files have the same number of elements
Tmp = importdata(files(k).name);
Out = zeros( numel( Tmp.data ), numel( files );
Out( :, 1 ) = Tmp.data(:);
for k = 2 : 1 : numel(files);
Tmp = importdata(files(k).name);
Out( : , k ) = Tmp.data(:);
end

3 commentaires

Can you please explain this code to me.I ran this code and it gave me the result I was expecting but I didnt quite understand how it is done.
% Read the first file to define the number of element
Tmp = importdata(files(1).name);
% Create the output (allocation) and fill the first column
Out = zeros( numel( Tmp.data ), numel( files );
Out( :, 1 ) = Tmp.data(:);
% All other file ...
for k = 2 : 1 : numel(files);
% import data form file
Tmp = importdata(files(k).name);
% Fill output (each column)
Out( : , k ) = Tmp.data(:);
end

Connectez-vous pour commenter.

Plus de réponses (1)

You're overwriting B on each iteration.
I suggest to modify as follows:
for k = 1:numel(files);
B(k) = importdata(files(k).name);
end
And after the loop:
B = [B.data];

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by