How to add cell array as index in plot title

2 ビュー (過去 30 日間)
Yunwei
Yunwei 2024 年 2 月 1 日
移動済み: Dyuman Joshi 2024 年 2 月 1 日
Hello all,
I have been trying to get cell array (as index) to be part of the plot title.
figure
tlo = tiledlayout (3,1);
data = {a, b, c};
title_s={50;25;10};
for i = 1:numel(data)
t=title_s{i};
ax = nexttile(tlo);
histogram(ax, data{i},'BinWidth',1,'BinEdges',1:1:25)
title(['Example\_' num2str(t) 'm (8H)'],FontSize=16);
end
When I add the line of title I got the error 'Unable to use a value of type string as an index.
Can anyone help to make it work?
Thanks a lot!
  3 件のコメント
Yunwei
Yunwei 2024 年 2 月 1 日
Hi,
I attach the data l used. The MATLAB version l use is 2021b.
Dyuman Joshi
Dyuman Joshi 2024 年 2 月 1 日
Even with the data you have attached, the code works here without any error -
Have you copied and pasted the right code?
load("data.mat")
tlo = tiledlayout(3,1);
title_s = {50;25;10};
for i = 1:numel(title_s)
t=title_s{i};
ax = nexttile(tlo);
histogram(ax, 25*rand(1,100),'BinWidth',1,'BinEdges',1:1:25)
title(['Example\_' num2str(t) 'm (8H)'],FontSize=16);
end

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

回答 (2 件)

Catalytic
Catalytic 2024 年 2 月 1 日
編集済み: Catalytic 2024 年 2 月 1 日
I suspect that the code you've shown us is not the code that produces the error. I suspect you did this instead -
figure
tlo = tiledlayout(3,1);
data = {randn(1,100),2,3};
title={50;25;10};
for i = 1:numel(data)
t=title{i};
ax = nexttile(tlo);
histogram(ax, data{i},'BinWidth',1,'BinEdges',1:1:25);
title(['Example\_' num2str(t) 'm (8H)'],FontSize=16);
end
Unable to use a value of type string as an index.

Yunwei
Yunwei 2024 年 2 月 1 日
移動済み: Dyuman Joshi 2024 年 2 月 1 日
I close matlab then run the code again, and the error is gone.
So l guess the code is correct.
Thank you!

カテゴリ

Help Center および File ExchangeMatrices and Arrays についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by