Reputation:
I have a directory of excel files that I need to read into Matlab as an array, transpose the array and then export back to an excel file. The excel file arrays are 32626 x 153 so I don't think I can output to an excel file first. I was thinking of outputting to a *.txt
or *.csv
file then opening it in excel. The current code I have written is trying to write to a *.csv
file
I need the name of the output file to include part of the name of the input file. For example, the input file name is data.xlsx
and I need the name of the ouyput file to be data_transposed.csv
.
The following is the code I have already written to read the *.xlsx
files in the directory and save the data to an array, then transpose the array, then write the transposed array to a *.csv
file.
files = dir('*.xlsx');
for i = 1:length(files)
filename = files(i).name;
[path, name, extention] = fileparts(filename);
array = xlsread(filename);
transposearray = transpose(array);
csvwrite('outputfile.csv' , transposearray)
end
There are a few issues with this code:
'outputfile'
rather than part of the 'inputfilename_transpose'
as I need it to be. (see explanation above)'outputfile'
rather than part of the 'inputfilename_transpose'
as I need it to be. (see explanation above)
This code only writes the first *.xlsx
file in the directory to the output file. I know the loop is iterating over all the *.xlsx
files in the directory because, when I take the semi-colons off all the other lines and comment out the csvwrite
line, Matlab is ingesting the correct information.EDIT: The code below works to output a file for every input file as well as include the input file name in the output file name
files = dir('*.xlsx');
for i = 1:length(files)
filename = files(i).name;
[path, name, extention] = fileparts(filename);
array = xlsread(filename);
transposearray = transpose(array);
csvwrite(['outputfile',name,'.csv'] , transposearray)
end
Excel still will not read the output file as it has double the number of columns excel can open. How can I parse out the first half of the transposed columns into sheet 1 and the second half of the transposed columns into sheet 2 of the same excel workbook? I assume this means I now have to use xlswrite()
instead of csvwrite()
.
EDIT: There are too many columns of excel to read the file. Therefore, I must parse the data into two separate sheets in one worksheet. The first xlswrite needs to take the first 16,384 columns (with all 153 rows) and place the values in excel sheet 1 starting at A1. The second xlswrite needs to take column number 16,385 to the last column (with all 153 rows) and place the values in excel sheet 2 starting at A1. How do I do this?
Upvotes: 0
Views: 1426
Reputation: 2619
The code writes all the files in the directory all of then has the same name they are getting overwritten. Try this
files = dir('*.xlsx');
for i = 1:length(files)
filename = files(i).name;
[path, name, extention] = fileparts(filename);
array = xlsread(filename);
transposearray = transpose(array);
csvwrite([path,'\',name,'_transpose','.csv'] , transposearray)
end
Beware that dir
reads files in the order as given by OS which may not be always sorted by name even if you have sorted your files by name while viewing them in a folder
For EDIT part
files = dir('*.xlsx');
for i = 1:length(files)
filename = files(i).name;
[path, name, extention] = fileparts(filename);
array = xlsread(filename);
transposearray = transpose(array);
n = ceil(size(transposearray,2)/2);
transportarray_part1 = transposearray(:,1:n);
transposearray _part2 = transposearray(:,n+1:end);
xlswrite([path,'\',name,'_transpose','.csv'], transposearray_part1,1);
xlswrite([path,'\',name,'_transpose','.csv'], transposearray_part2,2);
end
Upvotes: 1