String Variables Feeding through a loop & Generating Variables in a loop

1 回表示 (過去 30 日間)
Abe
Abe 2012 年 9 月 30 日
Hi I am trying to run a simple program the fetches stock data from yahoo. But I am having two problems 1) In the for loop I am creating, I would like the loop to create a new variable for each stock pulled.
So data_goog, data_aapl,data_hal, etc. (or data_1, data_2...) How do I do that in the loop?
2) In the "get_yahoo_stockdata2()" function the first paramter is the stock ticker. So 'aapl', would be accepted in the first slot of the function. However, when I have the stock in string format fed through the fuction it gives me an error. What am I doing wrong?
CODE: stock_string={'goog','aapl','hal','bp'};
for i=1:length(stock_string) data_stock_string(i)=get_yahoo_stockdata2(stock_string(i),'1/05/2005',datestr(now));
end

回答 (2 件)

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 30 日
編集済み: Azzi Abdelmalek 2012 年 9 月 30 日
use stock_string{i}
{ } instead of ( )
stock_string{i} is cell class
stock_string(i) is char class
  3 件のコメント
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 30 日
編集済み: Azzi Abdelmalek 2012 年 9 月 30 日
No it dos'nt
try this code
for k=1:5
eval([sprintf('variable%d=',k) num2str(k+5)])
end
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 9 月 30 日
編集済み: Azzi Abdelmalek 2012 年 9 月 30 日
but if you do (which is recommanded)
for i=1:5
variable_{i}=i+5
end
that means
variable_{1}=6
variable_{2}=7
variable_{3}=8

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


Walter Roberson
Walter Roberson 2012 年 9 月 30 日

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by