Borrar filtros
Borrar filtros

Reading one column from several text files and writing them to excell

1 visualización (últimos 30 días)
Hello Matlab community, I have several text files (attached above), each with two columns as shown below
That I would like The second column ((only)) to end up written on an Excel file. What I did is to save each one of the text files into a struc array. Then dealing with each part as a 2D array. I chose only the second column in the 2D array to be saved to the Excel file. However, I ended up stacking the columns from each file into one column instead of creating an Excel sheet with 3 columns! Please check the code below.
numfiles = 3; % total files to be imported
mydata = cell(1, numfiles); % % total number of reserved cells
FID=fopen('res.xls','w'); % file identifier
fprintf(FID,' SINR \n'); % just a lable where the users
for k = 1:numfiles % loop over all files to be imported
my_data_arrayed(:,:)=0; % creat a 2D arrat and fill it with zeros
myfilename = sprintf('%d.txt', k); % making a file pointer "myfilename"
mydata{k} = importdata(myfilename); % create a structural array
my_data_arrayed = mydata{1,k}.data; % save the struc to an array 2D
fprintf(FID,' %f \n',my_data_arrayed (:,2)); % copy the second column to Ecvel file
end
fprintf(FID,' \n');
fclose(FID);
The output that I am getting is shown in the attached picture
.
.
While what I need is as shown in the picture

Respuesta aceptada

KSSV
KSSV el 12 de Abr. de 2018
Editada: KSSV el 13 de Abr. de 2018
files = dir('*.txt') ;
N = length(files) ;
A = cell(1,N) ;
for i = 1:N
data = importdata(files(i).name) ;
A{i} = data.data(:,2) ;
end
A = cell2mat(A) ;
xlswrite('myfile.xls',A);
  3 comentarios
Mohammed Hadi
Mohammed Hadi el 13 de Abr. de 2018
Now it is working fine, thanks a lot @KSSV

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by