Clear Filters
Clear Filters

Export excel columns to multiple text files

2 views (last 30 days)
Majid Mohamod
Majid Mohamod on 16 May 2017
Edited: Majid Mohamod on 17 May 2017
I have excel file with multiple columns. I want to export each column to separated text file. So, I've 1650 columns and the output should be 1650 text file. There is anyway to do it in Matlab or any other method?
Thank you in advance! Majid

Answers (1)

KSSV
KSSV on 17 May 2017
You should be reading data from excel file using xlsread. Check the below code.
% data = xlsread('your excel file') ;
% rows = size(data,1) ;
rows = 100 ;
data = rand(rows,1650) ;
% run a loop to save each column into text file
for i = 1:1650
filename = strcat(num2str(i),'.txt') ;
data_col = data(:,i) ;
save(filename,'data_col','-ascii') ;
end
But still, I am surprised why you want each column into a text file though you have them in excel.
  2 Comments
Majid Mohamod
Majid Mohamod on 17 May 2017
I am preparing text files to be use in ArcSWAT. ArcSWAT read only a specific format of text files. I ran your code and it works. The problem is the output files is really different from the data on excel file!! This is the screenshot:
Majid Mohamod
Majid Mohamod on 17 May 2017
Edited: Majid Mohamod on 17 May 2017
On other hand, Walter Roberson had helped me to build a script. He also did the script for me of "Export excel columns to multiple text files" but it has simple problem which is the output has headers "x_1, x_2.......x_n). I'm including the screenshot of the output files here:
This is Walter Roberson's script:
data = readtable('YourFileName.xls');
varnames = data.Properties.VariableNames;
for col = 1 : size(data, 2)
thisvar = varnames{col};
filename = sprintf('split_%s.txt', thisvar);
writetable( data(:,col), filename );
end
The link of the answer is here
Please, could help to modify this script so can do the purpose?
Thanks, Majid

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!