For-Loop for multiple plots or histograms

32 ビュー (過去 30 日間)
Leonard Meyer
Leonard Meyer 2021 年 1 月 25 日
コメント済み: Leonard Meyer 2021 年 1 月 25 日
Dear all,
I am fairly new to matlab programming and I constantly struggle with tiresome repetitive constructs in my code. For example, I'd like to plot multiple histograms in a tiledlayout construct and apply the same xlim/ylim to each of them (see code below). Can someone indicate how i might use a loop to tidy up my code a bit and iterate through the histograms? Inputs in my code are different time series of returns, that are stored as individual variables.
figure
tiledlayout (1,3)
nexttile
histogram(SPNL)
xlim([-0.1 0.1])
ylim([0 800])
nexttile
histogram(EXNL)
xlim([-0.1 0.1])
ylim([0 800])
nexttile
histogram(NKNL)
xlim([-0.1 0.1])
ylim([0 800])
Thank you very much in advance!
Leo

採用された回答

Adam Danz
Adam Danz 2021 年 1 月 25 日
編集済み: Adam Danz 2021 年 1 月 25 日
Put the data into a cell array and then loop through the cell array.
figure
tlo = tiledlayout (1,3);
data = {SPNL, EXNL, NKNL};
for i = 1:numel(data)
ax = nexttile(tlo);
histogram(ax, data{i})
xlim(ax, [-0.1 0.1])
ylim(ax, [0 800])
end
  1 件のコメント
Leonard Meyer
Leonard Meyer 2021 年 1 月 25 日
Terrific! That works perfectly, thanks a lot.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Distribution Plots についてさらに検索

タグ

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by