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 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
F.
el 10 de Jul. de 2012
Editada: F.
el 10 de Jul. de 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 comentarios
F.
el 10 de Jul. de 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
Más respuestas (1)
Oleg Komarov
el 10 de Jul. de 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 comentarios
Ver también
Categorías
Más información sobre Characters and Strings en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!