Reading one column from several text files and writing them to excell
1 view (last 30 days)
Show older comments
Hello Matlab community, I have several text files (attached above), each with two columns as shown below
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172043/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172044/image.png)
.
.
While what I need is as shown in the picture
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/172045/image.png)
0 Comments
Accepted Answer
More Answers (0)
See Also
Categories
Find more on Spreadsheets in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!