Plotting content from structure
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
VBBV
on 26 May 2021
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
More Answers (0)
See Also
Categories
Find more on Text Data Preparation in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!