Effacer les filtres
Effacer les filtres

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

1 vue (au cours des 30 derniers jours)
Mohammed Hadi
Mohammed Hadi le 11 Avr 2018
Commenté : Mohammed Hadi le 13 Avr 2018
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

Réponse acceptée

KSSV
KSSV le 12 Avr 2018
Modifié(e) : KSSV le 13 Avr 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 commentaires
KSSV
KSSV le 13 Avr 2018
Edited the answer.....
Mohammed Hadi
Mohammed Hadi le 13 Avr 2018
Now it is working fine, thanks a lot @KSSV

Connectez-vous pour commenter.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by