writecell produces two cells when Range is a sigle cell

I am writing a since cell varialbe to an excel spreadsheet and the desired range changes each time. I decode the range as a selected Column (i.e., 'AX') concatenated to the row number i'm writing. Each time, the writecell produces the cell in the desired column and row, but also in the just previous column and row. The variable I'm writing is a single cell element. My code is:
Clm = cell2mat(ColmnName(jj));
Rng = [Clm num2str(irow)];
writecell(Cost,fnameout,'Sheet',1,'Range',Rng);
where, Clm = 'AE', irow = 31, Cost is a cell variable that changes each call, in this case Cost = 1×1 cell array / {[255.7800]}
writecell wrote to column AD and AE in row 31.

 Réponse acceptée

Stephen23
Stephen23 le 19 Juil 2026
Modifié(e) : Stephen23 le 19 Juil 2026
Try this:
Rng = sprintf('%1$s%2$d:%1$s%2$d', ColmnName{jj}, irow);
writecell(Cost, fnameout, 'Sheet', 1, 'Range', Rng);
Learn how to use curly brace indexing, rather than relying on awkward CELL2MAT:

2 commentaires

Yes, that did work.. Thank you.
Mike S
@Mike: i hope that it helped. To show your appreciation please remember to click the accept button.

Connectez-vous pour commenter.

Plus de réponses (0)

Produits

Version

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by