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?
11 次查看(过去 30 天)
显示 更早的评论
Writting data into an excel file columns in side a for loop
1 个评论
回答(1 个)
prabhat kumar sharma
2023-10-19
Hi Sameer,
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);
end
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!
1 个评论
Stephen23
2023-10-19
编辑:Stephen23
2023-10-19
"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')."
No, that incorrect statement mixes up R1C1 and A1 style references that Excel uses:
With R1C1 references the column "index" is numeric (as the Microsoft documentation explains) and the column letter does not change, it is always simply "C". Nothing in your answer has anything to do with R1C1 style referencing.
@prabhat kumar sharma: did you use an AI engine to generate this answer?
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Import from MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!