How to change the variable name in the loop

I want to use this syntax in a loop, but i can't find out why it does not work. I know, I should use the eval function, but somewhere there has to be a problem in the syntax. Could you help me to fix it?
thanks, Roley
I want to change te i variable in the code:
for to i=1:2 eval(['xlswrite('D:\Pénzügyek\Market\Trading System\Neural networks\FORECASTING MODELL\Modells\' num2srt(i) '.xlsx', modell ' num2srt(i)', 'data','A1')])
end

 採用された回答

Anuj
Anuj 2014 年 2 月 28 日

0 投票

You should edit your code using {}code while posting the question, I can see one problem, there should not be to after for , Also its better to use i1 in place of i because i represents imaginary number in MATLAB.

その他の回答 (4 件)

Jos (10584)
Jos (10584) 2014 年 2 月 28 日

1 投票

These kind of problems can be easily avoided using another approach
In pseudocode:
data(1).values = ...
data(2).values = ...
for K = 1:numel(data)
filename = ['d:\test\model' num2srt(K) '.xlsx']
xlswrite(filename, data(K).values)
end

1 件のコメント

Stephen23
Stephen23 2015 年 1 月 4 日
Seconded. Avoid eval, and stick with structs and cell arrays.

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

Chandrasekhar
Chandrasekhar 2014 年 2 月 28 日

0 投票

for to i=1:2
str = ['D:\Pénzügyek\Market\Trading System\Neural networks\FORECASTING MODELL\Modells\' num2str(i) '.xlsx, modell' num2str(i) ', data, ''A1'''];
eval(['xlswrite(' str ')'])
end
Iain
Iain 2014 年 2 月 28 日

0 投票

for to i = 1:2
The "to" should be removed.
The string you're trying to evaluate is nutty.
To form a string like the one you want:
['xlswrite(''d:\folder\folder\filename' num2str(i) '.xls'', modell' num2str(i) ', ''data'',''A1'')]
If you want a single ' in a string, you need to use two of them. i.e. '''' gives you a string with '
Roland
Roland 2014 年 2 月 28 日

0 投票

Thanks for the help. It works!

カテゴリ

ヘルプ センター および File ExchangeApp Building についてさらに検索

質問済み:

2014 年 2 月 28 日

コメント済み:

2015 年 1 月 4 日

Community Treasure Hunt

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

Start Hunting!

Translated by