필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
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

채택된 답변

KSSV
KSSV 2018년 4월 12일
편집: KSSV 2018년 4월 13일
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
Mohammed Hadi
Mohammed Hadi 2018년 4월 13일
Now it is working fine, thanks a lot @KSSV

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Spreadsheets에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by