フィルターのクリア

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

1 回表示 (過去 30 日間)
Mohammed Hadi
Mohammed Hadi 2018 年 4 月 11 日
コメント済み: Mohammed Hadi 2018 年 4 月 13 日
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 件のコメント
KSSV
KSSV 2018 年 4 月 13 日
Edited the answer.....
Mohammed Hadi
Mohammed Hadi 2018 年 4 月 13 日
Now it is working fine, thanks a lot @KSSV

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSpreadsheets についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by