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.

1 次查看(过去 30 天)
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

采纳的回答

F.
F. 2012-7-10
编辑:F. 2012-7-10
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 个评论
F.
F. 2012-7-10
% 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

请先登录,再进行评论。

更多回答(1 个)

Oleg Komarov
Oleg Komarov 2012-7-10
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];

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by