Error plotting a bar graph because Assigning to 2 elements using a simple assignment statement is not supported.

73 views (last 30 days)
Dear Mathcoders,
I have been striving with this code, but I don find the way to make it without producing this error, I have followed the indications of MATLAB and I used "deal" to make the comma list separated, however It still send me the same error. Do you mind giving me any insights to overcome this issue?
"Assigning to 2 elements using a simple assignment statement is not supported. Consider using comma-separated list
assignment.
Error in PGeo__SCy (line 383)
b2.BarWidth = 0.4;"
figure(1)
n=12;
U1=[energy_, (heat_cons + HDW_c)]; %%%Renewable generation
U2=[(Power_ + Sp_ + Lamp_), (heat_cons_ + HDW_c)*SPF]; %%%Consumption
c{1}=deal(U1); %%%Renewable generation
c{2}=deal(U2); %%%Consumption
b1= bar(c{1},'stacked');
b1(1).BarWidth = 0.4;
hold on;
b2=bar(c{2},'stacked');
b2.BarWidth = 0.4;
b2.XData = (1:n) - 0.5; % move bars left
xlabel('Months', 'FontWeight','bold')
ylabel('Consumption vs RES (kWp)', 'FontWeight','bold')
grid on
xticklabels(LastName)
% go.Annotation.LegendInformation.IconDisplayStyle = 'on';
labels = {'Electricity consumption' 'Thermal consumption' 'Photovoltaic Generation' 'Geothermal Generation' };
legend([b2 b1], labels);
%
% saveas(gcf,'_EnergyShare.png')

Accepted Answer

Walter Roberson
Walter Roberson on 9 Sep 2021
bar() returns one object of class Bar for each column in the input.
b1= bar(c{1},'stacked');
b1(1).BarWidth = 0.4;
That is okay because in the assignment you select down to just the first of the objects returned.
b2=bar(c{2},'stacked');
b2.BarWidth = 0.4;
if c{2} has more than one column then b2 is a vector of Bar objects, and attempting to assign to b2.BarWidth is as-if you had written
b2(1).BarWidth, b2(2).BarWidth, b2(3).BarWidth = 0.4
except with the comma not acting as a statement separation. It is a syntax error.
In the b1 case you selected just the first bar; if you wanted the same for the second then you should have assigned to b2(1).BarWidth .
If you want to assign to the bar width of all of the b2 objects, then you need to do
set(b2, 'BarWidth', 0.4)

More Answers (0)

Categories

Find more on Labels and Annotations in Help Center and File Exchange

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!