Second plot overwrites first plot in function script

조회 수: 23 (최근 30일)
JB
JB 2022년 10월 25일
댓글: JB 2022년 10월 25일
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")

채택된 답변

Cris LaPierre
Cris LaPierre 2022년 10월 25일
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
JB
JB 2022년 10월 25일
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.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Pie Charts에 대해 자세히 알아보기

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by