フィルターのクリア

how to execute a loop through all the variables in the workspace?

18 ビュー (過去 30 日間)
Roberta
Roberta 2014 年 10 月 5 日
コメント済み: Roberta 2014 年 10 月 5 日
Hi all,
Here's my question:
I ha about 300 variables in my workspace, which are named accordingly to the contents (are macroeconomic time series for 20 countries)
for example:
  • gdpUK (is the GDP of United Kingdom)
  • gdpUS (is the GDP of United States)
  • ... (and so on for 20 countries)
  • reerUK (is the real effective exchange rate for the United Kingdom)
  • reerUS (REER for US)and so on
I would like to standardise these series, i need something that do:
for i= first variable: last variable
i=(i-mean(i))/std(i)
end
how can I do this?
thank you in advance for your help! :)

採用された回答

Guillaume
Guillaume 2014 年 10 月 5 日
編集済み: Guillaume 2014 年 10 月 5 日
It would have been better to store your data in tables or even cell arrays or maps. Anyway:
for cv = who('gdp*')' %transpose to get a row cell array
eval(sprintf('mean%1$s = (%1$s - mean(%1$s)) / std(%1$s);', cv{1})); %1$s is replaced by var name, output is named meanvarname
end
%same with 'reer*'
  1 件のコメント
Roberta
Roberta 2014 年 10 月 5 日
Thank you very much!
It solved the problem even if, as you said, it isn't certainly the most efficient solution. Actually I was looking for a quick&dirty solution, and I've found it!
Once more, thank you!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLogical についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by