Second plot overwrites first plot in function script

16 views (last 30 days)
I wrote a function that is supposed to plot two separate pie charts. It, however, does not seem to do this within the function script. If I copy and paste the code in the live script it works. I'm at a loss as to why it won't work. Any suggestions are welcome to improve or fix this code.
Here is the relevant portion of the code:
%Creates blank string to start compiling location names to make legend
blank = strings(1, length(grocLocCats));
%Creates blank vector for compiling cost numbers
blankCost = zeros(length(grocLocCats), 1);
for k = 1:length(grocLocCats)
y = grocLocCats(k);
locIdx = strcmp(Table.Location, y);
cost = sum(Table.Cost(locIdx));
blankCost(k) = cost;
costStr= num2str(cost, '%0.2f');
numStr = strcat(y ," = ", "$", costStr);
% To make string for pie chart legend
blank(k) = numStr(1);
end
%% Makes pie chart with labels and legend
figure(1); % figure function required to plot more than one figure in a script
pie(blankCost)
grocSumStr = num2str(sum(Table.Cost(grocLocIdx)));
monthName = string(month(datetime(theDate,'InputFormat','uuuu-MM-dd'), 'name'));
yearNum = string(year(datetime(theDate)));
title( "Location Expenditure for " + monthName + " " + yearNum + " (" + "$" + grocSumStr + ")")
legend(blank, "Location",'southoutside')
hold off
%%%%%%%%%%%%%%%%%%%%%%% Items Section %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Blank vector to store Item totals
itemBlank = zeros(length(grocLocCats),1);
%% Blank string to store Location and Item concatenated strings
itemMTStr = strings(1, length(grocLocCats));
for k = 1 : length(grocLocCats);
y = grocLocCats(k);
locIdx2 = strcmp(Table.Location,y);
locItNum = sum(Table.ItemCount(locIdx2));
itemBlank(k) = locItNum;
itemNumStr = num2str(locItNum);
itemLegStr = strcat(y, "-",itemNumStr);
itemMTStr(k)= itemLegStr(1);
end
%% Makes pie chart for Items No.
figure(2); % figure function required to plot more than one figure in a script
pie(itemBlank)
title("Total No. of Items at Grocery Locations" + " " + monthName + " " + yearNum);
legend(itemMTStr,"Location", "southoutside")

Accepted Answer

Cris LaPierre
Cris LaPierre on 25 Oct 2022
I think this has to do with how live scripts handle figures. The fix is to remove the (1) and (2) after the figure commands in your function.
% Creating a dummy data set for testing
grocLocCats = ["cat1";"cat2"];
grocLocIdx = 1:2;
theDate = datetime('now');
Cost = [15;25];
ItemCount = [122;34];
Table = table(grocLocCats,Cost,ItemCount,'VariableNames',["Location","Cost","ItemCount"]);
test(Table,grocLocCats,grocLocIdx,theDate)
% Place the code in a function to reproduce the reported use case.
function test(Table,grocLocCats,grocLocIdx,theDate)
%Creates blank string to start compiling location names to make legend
blank = strings(1, length(grocLocCats));
%Creates blank vector for compiling cost numbers
blankCost = zeros(length(grocLocCats), 1);
for k = 1:length(grocLocCats)
y = grocLocCats(k);
locIdx = strcmp(Table.Location, y);
cost = sum(Table.Cost(locIdx));
blankCost(k) = cost;
costStr= num2str(cost, '%0.2f');
numStr = strcat(y ," = ", "$", costStr);
% To make string for pie chart legend
blank(k) = numStr(1);
end
%% Makes pie chart with labels and legend
figure % <----------------------------------------------------------------####### remove (2)
pie(blankCost)
grocSumStr = num2str(sum(Table.Cost(grocLocIdx)));
monthName = string(month(datetime(theDate,'InputFormat','uuuu-MM-dd'), 'name'));
yearNum = string(year(datetime(theDate)));
title( "Location Expenditure for " + monthName + " " + yearNum + " (" + "$" + grocSumStr + ")")
legend(blank, "Location",'southoutside')
hold off
%%%%%%%%%%%%%%%%%%%%%%% Items Section %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% Blank vector to store Item totals
itemBlank = zeros(length(grocLocCats),1);
%% Blank string to store Location and Item concatenated strings
itemMTStr = strings(1, length(grocLocCats));
for k = 1 : length(grocLocCats);
y = grocLocCats(k);
locIdx2 = strcmp(Table.Location,y);
locItNum = sum(Table.ItemCount(locIdx2));
itemBlank(k) = locItNum;
itemNumStr = num2str(locItNum);
itemLegStr = strcat(y, "-",itemNumStr);
itemMTStr(k)= itemLegStr(1);
end
%% Makes pie chart for Items No.
figure % <----------------------------------------------------------------####### remove (2)
pie(itemBlank)
title("Total No. of Items at Grocery Locations" + " " + monthName + " " + yearNum);
legend(itemMTStr,"Location", "southoutside")
end
  1 Comment
JB
JB on 25 Oct 2022
Thanks! This fix doesn't seem inituitive though. I would think I would need to distinguish the figures with numbers. I also searched previous answers that actually recommended including the numbers.

Sign in to comment.

More Answers (0)

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by