How to increment the column in a for loop (When i = 1 column is Z1, when i =2 column is AC1)? Is there any commands that support R1C1 format of indexing excel cells?
8 次查看（过去 30 天）
prabhat kumar sharma 2023-10-19
I understand that you are facing an issue with writing Excel data to a specific row and column in the sheet. Additionally, the (Row, Column) values should be different for each iteration of the 'for' loop.
Here's an example that addresses your requirement:
filename = 'prabhat_data.xlsx'
sheet = 1
data = [1 2 3 4 5];
for i = 1:numel(data)
column = char('A' + i - 1);
cell = [column '1'];
T = table(data(i));
writetable(T, filename, 'Sheet', sheet, 'Range', cell);
In this code, we generate the column letter by adding the loop index i to the ASCII value of the letter 'A' and subtracting 1. This gives us the corresponding column letter in Excel.
The 'R1C1' format cell reference is created by concatenating the column letter with the row number (in this case, '1'). If you want to change the row, you can modify the code accordingly.
We create a table T with the data value (data(i)) using the table function.
Then, we use the ‘writetable’ function to write the table to the specified cell (cell) in the Excel file, specifying the filename, sheet, and range.
For more information on the ‘writetable’ function, you can refer to the following documentation
I hope it helps!