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 view (last 30 days)
Show older comments
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
0 Comments
Accepted Answer
F.
on 10 Jul 2012
Edited: F.
on 10 Jul 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 Comments
F.
on 10 Jul 2012
% 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
More Answers (1)
Oleg Komarov
on 10 Jul 2012
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];
0 Comments
See Also
Categories
Find more on Structures in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!