Plotting content from structure
1 回表示 (過去 30 日間)
古いコメントを表示
Hey, I am trying to plot values from a structure but somehow am getting Error using startswith.
Here is the code (examplified):
sges.location1 = 1;
sges.location2 = 2;
sges.location3 = 3,
sges.location4 = 4;
sges.location5 = 5;
sh.location1 = 4;
sh.location2 = 1;
sh.location3 = 2,
sh.location4 = 5;
sh.location5 = 3;
w1 = 0.6;
x = [1 2 3 4 5];
bar(x,sges,'FaceColor',[0.2 0.2 0.5])
ax = gca;
ax.YLabel.String = "Distance (km)"
title('Distance')
w2 = 0.5;
hold on
bar(x,sh,w2, 'FaceColor',[0 0.7 0.7])
grid on
legend({'sges', 'sh'}, 'Location', 'northwest')
labels = {'Loc1', 'Loc2', 'Loc3', 'Loc4', 'Loc5'};
set(gca, 'xtick', 1:5, 'XTickLabels', labels);
What am I doing wrong, am I missing something?
0 件のコメント
採用された回答
VBBV
2021 年 5 月 26 日
sges.location1 = 1;
sges.location2 = 2;
sges.location3 = 3,
sges.location4 = 4;
sges.location5 = 5;
sh.location1 = 4;
sh.location2 = 1;
sh.location3 = 2,
sh.location4 = 5;
sh.location5 = 3;
w1 = 0.6;
x = [1 2 3 4 5];
C = struct2cell(sges)
cs = cell2mat(C)
H = struct2cell(sh)
hs = cell2mat(H)
bar(x,cs,'FaceColor',[0.2 0.2 0.5])
ax = gca;
ax.YLabel.String = "Distance (km)"
title('Distance')
w2 = 0.5;
hold on
bar(x,hs,w2, 'FaceColor',[0 0.7 0.7])
grid on
legend({'sges', 'sh'}, 'Location', 'northwest')
labels = {'Loc1', 'Loc2', 'Loc3', 'Loc4', 'Loc5'};
set(gca, 'xtick', 1:5, 'XTickLabels', labels);
Use struct2cell and cell2mat for converting the struct array to double
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Text Data Preparation についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!