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!