Determine excel range in writetable function
31 views (last 30 days)
Show older comments
I have multiple tables t1 t2...t45 and i want to write them on one sheet in excel using write table. All of them have the same number of rows but different number of columns. for example t1=6000x4 whereas t2=6000x3.How can I write these tables in consecutive columns in excel?
In other words, t1 range is A1:D6000 but I want t2 to be E1:G6000. So, I can can solve it using:
writetable(t1,'myfile.xlsx','Sheet',1,'Range','A1:D6000')
writetable(t2,'myfile.xlsx','Sheet',1,'Range','E1:G6000')
but how can i generate the range automatically with the 45 tables. some tables have 78 columns which makes it very hard to determine the range manually.
0 Comments
Answers (1)
Image Analyst
on 5 Feb 2017
Use ExcelCol at http://www.mathworks.com/matlabcentral/fileexchange/27182-excel-column-conversion to convert column number to a letter or pair of letters. Then do
column = 1;
columnLetters = char(ExcelCol(column));
% Convert to Excel A1 format
cellReference = sprintf('%s1', columnLetters);
% Write the table
writetable(t1,'myfile.xlsx','Sheet',1,'Range', cellReference)
% Go to next column, skipping a column.
column = column + size(t1, 2); % Change the variable name inside size().
% Should be the prior variable, not the current one you're about to write.
cellReference = sprintf('%s1', columnLetters);
writetable(t2,'myfile.xlsx','Sheet',1,'Range',cellReference)
% Go to next column, skipping a column.
column = column + size(t2, 2); % Change the variable name inside size().
% Should be the prior variable, not the current one you're about to write.
cellReference = sprintf('%s1', columnLetters);
writetable(t3,'myfile.xlsx','Sheet',1,'Range',cellReference)
% And so on....
0 Comments
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!