Second plot overwrites first plot in function script

23 visualizaciones (últimos 30 días)
JB
JB el 25 de Oct. de 2022
Comentada: JB el 25 de Oct. de 2022
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")

Respuesta aceptada

Cris LaPierre
Cris LaPierre el 25 de Oct. de 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 comentario
JB
JB el 25 de Oct. de 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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Pie Charts en Help Center y File Exchange.

Productos


Versión

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by