How can I use use the index in a FOR loop in a string?

I need use cycle FOR for String (for Excel write).
For example:
N=10;
for i = 1:N-1
xlswrite('test.xls',F0,brd{i},'A1') %I need change 1 to 2...N
end
How can I do that?

1 件のコメント

Todd Flanagan
Todd Flanagan 2011 年 1 月 20 日
Hi Pepa. I deleted your "Thanks" answer and added it as a comment to Andreas' answer.

サインインしてコメントする。

 採用された回答

Andreas Goser
Andreas Goser 2011 年 1 月 20 日

3 投票

While I suggest using the XLSWRITE syntax,
xlswrite('test.xls',F0,brd{i},'A1:A10')
this may just an example code. I personally often work with the EVAL command:
N=10;
for i = 1:N-1
eval(['xlswrite(''test.xls'',F0,brd{i},''A', num2str(i), ''');'])
end
I think there are other options too.

5 件のコメント

Matt McDonnell
Matt McDonnell 2011 年 1 月 20 日
An alternative that doesn't use eval:
xlswrite('test.xls',F0,brd{i},sprintf('A%d',i));
inside the loop
Walter Roberson
Walter Roberson 2011 年 1 月 20 日
eval() ? Oh, gross!!
http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
Andreas Goser
Andreas Goser 2011 年 1 月 20 日
Yeah, some think EVAL is EVIL - like GOTO commands :-)
Todd Flanagan
Todd Flanagan 2011 年 1 月 20 日
Pepa says, "Thank you!!!"
Doug Hull
Doug Hull 2011 年 1 月 20 日
EVAL *is* EVIL! If you agree, vote for the alternative answer below.

サインインしてコメントする。

その他の回答 (3 件)

Doug Hull
Doug Hull 2011 年 1 月 20 日

11 投票

I think this is the cleanest way to do this.
N=10;
for i = 1:N-1
colName = ['A' num2str(i)];
xlswrite('test.xls',F0,brd{i}, colName) %I need change 1 to 2...N
end
Walter Roberson
Walter Roberson 2011 年 1 月 20 日

1 投票

Or for those who prefer the lower-overhead sprintf:
N=10;
for i = 1:N-1
xlswrite('test.xls',F0,brd{i}, sprintf('A%d',i))
end

1 件のコメント

Jan
Jan 2011 年 1 月 26 日
Thanks Walter! As lean as possible.

サインインしてコメントする。

Richard de Garis
Richard de Garis 2011 年 1 月 26 日

0 投票

The above solutions are elegantly simple and will serve your current need perfectly. If in the future you require to iterate along Excel columns and/or change the size of the Excel range, I just published a function that will enable you to do that. http://www.mathworks.com/matlabcentral/fileexchange/30180-calculate-excel-range

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by