pie chart creation with name (number) and corresponding percentage

Hi! I would like to create a pie chart like in the present demo:
x = [1,2,3];
p = pie(x);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
txt = {'Item A: ';'Item B: ';'Item C: '};
combinedtxt = strcat(txt,percentValues);
pText(1).String = combinedtxt(1);
pText(2).String = combinedtxt(2);
pText(3).String = combinedtxt(3);
How can I achieve the same result with my data?
I tried it this way:
number = [78;79;80;81;82;83;84;85;86;87;88]';
value = [4509;5239;6400;9074;11047;13147;15137;13909;6354;1152;183]';
number2 = {};
for K = 1:height(number)
number1 = sprintf('%.0f',number(K));
number2 = [number2,{number1}];
end
number2 = number2.';
p = pie(value);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
combinedtxt = strcat(number2,percentValues);
for K = 1:height(value)
pText(K).String = combinedtxt(K);
end

 Accepted Answer

One approach —
number = [78;79;80;81;82;83;84;85;86;87;88]';
value = [4509;5239;6400;9074;11047;13147;15137;13909;6354;1152;183]';
f = figure;
p = pie(value);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
delete(f)
pcts = regexp(percentValues, '\d*', 'match');
pcts = cellfun(@(x)str2double(x), pcts);
idx = pcts<7;
combine = number(idx);
pctv = pcts(idx);
percentVals = compose(' (%d%%)',pcts(~idx));
percentVals{end+1} = [' (' num2str(sum(pctv)) '%)'];
nrc = compose('%d ', number(~idx)).';
nrc{end+1} = [num2str(combine) ' '];
cattxt = cellfun(@(x,y)cat(2,x,y),nrc, percentVals, 'Unif',0);
pText(numel(cattxt)+1:end) = [];
value2 = [value(~idx) sum(value(idx))];
figure
p = pie(value2);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pText(k).String = cattxt(k);
end
.

15 Comments

Thank you for your reply @Star Strider. Do you also know how to make the following change?
Also, is it possible to determine the color for each piece?
For example, use:
color = [1 0 0; 0 1 0; 0 0 1; ....];
As always, my pleasure!
That simply requires a few modifications of my current code (and a reference to the pie documentation section on Compare Two Pie Charts to add the legend).
Try this —
number = [78;79;80;81;82;83;84;85;86;87;88]';
value = [4509;5239;6400;9074;11047;13147;15137;13909;6354;1152;183]';
f = figure;
p = pie(value);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
delete(f)
pcts = regexp(percentValues, '\d*', 'match');
pcts = cellfun(@(x)str2double(x), pcts);
idx = pcts<7;
combine = number(idx);
pctv = pcts(idx);
percentVals = compose(' (%d%%)',pcts(~idx));
percentVals{end+1} = [' (' num2str(sum(pctv)) '%)'];
nrc = compose('%d ', number(~idx)).';
nrc{end+1} = [num2str(combine) ' '];
cattxt = cellfun(@(x,y)cat(2,x,y),nrc, percentVals, 'Unif',0); % Originally Segment Labels, Now 'legend' Text
pText(numel(cattxt)+1:end) = [];
value2 = [value(~idx) sum(value(idx))];
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
pText(k).String = []; % Delete The 'String' Labels
end
legend(cattxt, 'Location','eastoutside')
.
Hi @Star Strider. Perfect!
I also wanted to ask you for two other separate pieces of information:
1. I would like to change the legend of the chart created like this:
2. I asked a new question in this post. It is a continuation to this discussion.
Thank you!
That requires replacing this assignment:
nrc{end+1} = [num2str(combine) ' ']
with this one:
nrc{end+1} = sprintf('%d %d\n%d %d ',combine); % NEW 'nrc{end+1}'
or this one:
nrc{end+1} = sprintf('%d %d (%2d%%)\n%d %d',[combine(1:2) sum(pcts(idx)) combine(3:4)]) % NEW 'nrc{end+1}'
To get different versions, change the format specifier string.
I changed the others as well, since with a new compose call and with sprintf for the last element in ‘nrc’, ‘cattxt’ is no longer necessary. To avoid re-writing the rest of the code, I just set:
cattxt = nrc;
Try this —
number = [78;79;80;81;82;83;84;85;86;87;88]';
value = [4509;5239;6400;9074;11047;13147;15137;13909;6354;1152;183]';
f = figure;
p = pie(value);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
delete(f)
pcts = regexp(percentValues, '\d*', 'match');
pcts = cellfun(@(x)str2double(x), pcts);
idx = pcts<7;
combine = number(idx);
pctv = pcts(idx);
percentVals = compose(' (%d%%)',pcts(~idx));
percentVals{end+1} = [' (' num2str(sum(pctv)) '%)'];
nrc = compose('%d (%2d%%)', [number(~idx).', pcts(~idx)]);
% return
% nrc{end+1} = [num2str(combine) ' ']
nrc{end+1} = sprintf('%d %d (%2d%%)\n%d %d',[combine(1:2) sum(pcts(idx)) combine(3:4)]); % NEW 'nrc{end+1}'
% cattxt = cellfun(@(x,y)cat(2,x,y),nrc, percentVals, 'Unif',0); % Originally Segment Labels, Now 'legend' Text
cattxt = nrc;
pText(numel(cattxt)+1:end) = [];
value2 = [value(~idx) sum(value(idx))];
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
pText(k).String = []; % Delete The 'String' Labels
end
legend(cattxt, 'Location','eastoutside')
.
Thank you! While regarding the position of the numbers 78 79 87 88, is it possible for me to decide how to arrange them? I would like to decide how to arrange them, like 1x4, 2x2, 3x1, 4x1. (see central legend: arranged 3x1)
I tried editing this line of code but it doesn't seem to be the right move.
nrc{end+1} = sprintf('%d %d (%2d%%)\n%d %d',[combine(1:2) sum(pcts(idx)) combine(3:4)]);
Also, is it possible to align the numbers with the colored box (as for the other cases)? (see last legend)
As always, my pleasure!
The last legend entry is controlled by this line:
nrc{end+1} = sprintf('%d %d (%2d%%)\n%d %d',[combine(1:2) sum(pcts(idx)) combine(3:4)]); % NEW 'nrc{end+1}'
so you can change the format descriptor string to an extent, for example:
nrc{end+1} = sprintf('%d %d %d (%2d%%)\n%d %d',[combine(1:3) sum(pcts(idx)) combine(4)]); % NEW 'nrc{end+1}'
however the placement with respect to the associated coloured patch bar is likely not an option. It has to be like one of the first two images. (My source for such information is Undocumented MATLAB and it has no information on this.) Adding an ‘invisible’ patch like that to the legend may not be possible without also adding it to the pie chart. The legend function has an undocumented second output that allows for some slight changes, however not what you want to do (I did that experiment, and it failed).
It is possible to get them to line up like you want by changing the edit descriptor string in ‘nrc{end+1}’, (adding a leading '\n') however that leaves a gap above the last patch
number = [78;79;80;81;82;83;84;85;86;87;88]';
value = [4509;5239;6400;9074;11047;13147;15137;13909;6354;1152;183]';
f = figure;
p = pie(value);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
delete(f)
pcts = regexp(percentValues, '\d*', 'match');
pcts = cellfun(@(x)str2double(x), pcts);
idx = pcts<7;
combine = number(idx);
pctv = pcts(idx);
percentVals = compose(' (%d%%)',pcts(~idx));
percentVals{end+1} = [' (' num2str(sum(pctv)) '%)'];
nrc = compose('%d (%2d%%)', [number(~idx).', pcts(~idx)]);
% return
% nrc{end+1} = [num2str(combine) ' ']
nrc{end+1} = sprintf('\n%d %d (%2d%%)\n%d %d',[combine(1:2) sum(pcts(idx)) combine(3:4)]); % NEW 'nrc{end+1}'
% cattxt = cellfun(@(x,y)cat(2,x,y),nrc, percentVals, 'Unif',0); % Originally Segment Labels, Now 'legend' Text
cattxt = nrc;
pText(numel(cattxt)+1:end) = [];
value2 = [value(~idx) sum(value(idx))];
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
pText(k).String = []; % Delete The 'String' Labels
end
legend(cattxt, 'Location','eastoutside')
.
Thanks again for the answer @Star Strider! You have been a great help!
So it is not possible to get the provisions in the following way? That is, by modifying R and C.
Only for values ​​with <7%!
As always, my pleasure!
You just need to change the format descriptor in the sprintf call in ‘nrc{end+1}’ in various ways —
number = [78;79;80;81;82;83;84;85;86;87;88]';
value = [4509;5239;6400;9074;11047;13147;15137;13909;6354;1152;183]';
f = figure;
p = pie(value);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
delete(f)
pcts = regexp(percentValues, '\d*', 'match');
pcts = cellfun(@(x)str2double(x), pcts);
idx = pcts<7;
combine = number(idx);
pctv = pcts(idx);
percentVals = compose(' (%d%%)',pcts(~idx));
percentVals{end+1} = [' (' num2str(sum(pctv)) '%)'];
nrc = compose('%d (%2d%%)', [number(~idx).', pcts(~idx)]);
% return
% nrc{end+1} = [num2str(combine) ' ']
nrc{end+1} = sprintf('\n%d %d %d (%2d%%)\n%d',[combine(1:2) sum(pcts(idx)) combine(3:4)]); % NEW 'nrc{end+1}'
% cattxt = cellfun(@(x,y)cat(2,x,y),nrc, percentVals, 'Unif',0); % Originally Segment Labels, Now 'legend' Text
cattxt = nrc;
pText(numel(cattxt)+1:end) = [];
value2 = [value(~idx) sum(value(idx))];
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
pText(k).String = []; % Delete The 'String' Labels
end
legend(cattxt, 'Location','eastoutside')
% Previous 'nrc{end+1}' is now 'nrc{end}' In This Illustration —
nrc{end} = sprintf('\n\n\n%d (%2d%%)\n%d\n%d\n%d',[combine(1) sum(pcts(idx)) combine(2:4)]); % NEW 'nrc{end+1}'
cattxt = nrc;
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
pText(k).String = []; % Delete The 'String' Labels
end
legend(cattxt, 'Location','eastoutside')
% Previous 'nrc{end+1}' is now 'nrc{end}' In This Illustration —
nrc{end} = sprintf('%d %d %d %d (%2d%%)',[combine sum(pcts(idx))]); % NEW 'nrc{end+1}'
cattxt = nrc;
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
pText(k).String = []; % Delete The 'String' Labels
end
legend(cattxt, 'Location','eastoutside')
That is as close as I can get the last element to what you want. I cannot do anything about the extra spaces.
.
OK, the result is good enough for me!
One last info: with a different dataset I have a problem regarding the display of <7% values in the legend.
I tried using the line of code you showed me earlier but I get a layout that is not correct. I'll leave you the code I used:
number = [48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88];
value = [0 0 0 0 0 0 42 91 152 276 440 572 821 1155 1580 1761 2157 2256 2578 2499 2715 2794 3280 3237 3263 3586 3334 3652 3675 4404 4509 5239 6400 9074 11047 13147 15137 13909 6354 1152 183];
f = figure;
p = pie(value);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
delete(f)
pcts = regexp(percentValues, '\d*', 'match');
pcts = cellfun(@(x)str2double(x), pcts);
idx = pcts<7;
combine = number(idx);
pctv = pcts(idx);
percentVals = compose(' (%d%%)',pcts(~idx));
percentVals{end+1} = [' (' num2str(sum(pctv)) '%)'];
nrc = compose('%d (%2d%%)', [number(~idx).', pcts(~idx)]);
% VARIED
nrc{end+1} = sprintf('%d %d %d %d %d (%2d%%)\n%d %d %d %d %d',[combine(1:5) sum(pcts(idx)) combine(6:end)]); % NEW 'nrc{end+1}'
cattxt = nrc;
pText(numel(cattxt)+1:end) = [];
value2 = [value(~idx) sum(value(idx))];
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
pText(k).String = []; % Delete The 'String' Labels
end
lgd = legend(cattxt, 'Location','eastoutside','FontSize',12);
I really wanted to get this:
Is this something workable?
Also, I noted that the total comes 107% (7+8+10+11+10+61).
Thank you!
You need to change ‘nrc{end+1}’ to accommodate the new ‘combine’ vector:
nrc{end+1} = sprintf('%d %d %d %d %d %d (%2d%%)\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d',[combine(1:6) sum(pcts(idx)) combine(7:end)]); % NEW 'nrc{end+1}'
Here, I used a (6x6) array for the numbers, since ‘combine’ is now a (1x36) vector. Change it to do whatever works best for you.
Try this —
number = [48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88];
value = [0 0 0 0 0 0 42 91 152 276 440 572 821 1155 1580 1761 2157 2256 2578 2499 2715 2794 3280 3237 3263 3586 3334 3652 3675 4404 4509 5239 6400 9074 11047 13147 15137 13909 6354 1152 183];
f = figure;
p = pie(value);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
delete(f)
pcts = regexp(percentValues, '\d*', 'match');
pcts = cellfun(@(x)str2double(x), pcts);
idx = pcts<7;
combine = number(idx);
pctv = pcts(idx);
percentVals = compose(' (%d%%)',pcts(~idx));
percentVals{end+1} = [' (' num2str(sum(pctv)) '%)'];
nrc = compose('%d (%2d%%)', [number(~idx).', pcts(~idx)]);
% VARIED
nrc{end+1} = sprintf('%d %d %d %d %d %d (%2d%%)\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d',[combine(1:6) sum(pcts(idx)) combine(7:end)]); % NEW 'nrc{end+1}'
cattxt = nrc;
pText(numel(cattxt)+1:end) = [];
value2 = [value(~idx) sum(value(idx))];
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
pText(k).String = []; % Delete The 'String' Labels
end
lgd = legend(cattxt, 'Location','eastoutside','FontSize',12);
.
Thanks again! But why the sum of the % is 107 and not 100?
It should be:
81 (7%)
82 (8%)
83 (10%)
84 (11%)
85 (10%)
-------------
(46%)
48 49 ...... (54% and not 61%)
....... 86 87 88
That is probably due to accumulated rounding error.
That may not be possible. I tried my own version, and I don’t get exactly the same results the pie gets, although my calculations otherwise work —
number = [48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88];
value = [0 0 0 0 0 0 42 91 152 276 440 572 821 1155 1580 1761 2157 2256 2578 2499 2715 2794 3280 3237 3263 3586 3334 3652 3675 4404 4509 5239 6400 9074 11047 13147 15137 13909 6354 1152 183];
f = figure;
p = pie(value);
[U_value,nidx,ix] = unique(value,'stable');
Counts = accumarray(ix, value);
sum_Counts = sum(Counts);
Freqs = Counts/sum(Counts) * 100;
[v1,v2] = bounds(Freqs)
v1 = 0
v2 = 11.0917
number_v = number(nidx).';
Interim_Results = table(number_v,Counts,Freqs)
Interim_Results = 36×3 table
number_v Counts Freqs ________ ______ ________ 48 0 0 54 42 0.030776 55 91 0.066681 56 152 0.11138 57 276 0.20224 58 440 0.32241 59 572 0.41914 60 821 0.60159 61 1155 0.84633 62 1580 1.1578 63 1761 1.2904 64 2157 1.5806 65 2256 1.6531 66 2578 1.889 67 2499 1.8312 68 2715 1.9894
Freqs_lt_7 = Freqs < 7; % Threshold Results
Thresholded_Interim_Results_1 = sum(Freqs(Freqs_lt_7))
Thresholded_Interim_Results_1 = 60.9880
Thresholded_Interim_Results_2 = [number_v(~Freqs_lt_7) Freqs(~Freqs_lt_7) round(Freqs(~Freqs_lt_7))];
Thresholded_Interim_Results_2(end+1,:) = [NaN sum(Thresholded_Interim_Results_2(:,2:end))]
Thresholded_Interim_Results_2 = 5×3
82.0000 8.0948 8.0000 83.0000 9.6335 10.0000 84.0000 11.0917 11.0000 85.0000 10.1919 10.0000 NaN 39.0120 39.0000
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
delete(f)
pcts = regexp(percentValues, '\d*', 'match');
pcts = cellfun(@(x)str2double(x), pcts);
idx = pcts<7;
combine = number(idx);
pctv = pcts(idx);
percentVals = compose(' (%d%%)',pcts(~idx));
percentVals{end+1} = [' (' num2str(sum(pctv)) '%)'];
nrc = compose('%d (%2d%%)', [number(~idx).', pcts(~idx)]);
% VARIED
nrc{end+1} = sprintf('%d %d %d %d %d %d (%2d%%)\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d',[combine(1:6) sum(pcts(idx)) combine(7:end)]); % NEW 'nrc{end+1}'
cattxt = nrc;
pText(numel(cattxt)+1:end) = [];
value2 = [value(~idx) sum(value(idx))];
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
pText(k).String = []; % Delete The 'String' Labels
end
lgd = legend(cattxt, 'Location','eastoutside','FontSize',12);
.
@Alberto Acri: The numbers sum to 107 because the first (deleted) pie chart includes several strings that are "<1%". When you get the number from those strings and use it, of course those are counted as 1 instead of what they really are. The solution is, instead of getting the percentages from a pie chart you're going to delete, just calculate the percentages directly, and they'll have a higher precision. You'll have to round them when you use them in the legend, but here's the important part: the small ones (<1%) do not get "rounded" to 1 and then summed; instead you sum them first and then round the result. You can see below that the total sum is 100:
number = [48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88];
value = [0 0 0 0 0 0 42 91 152 276 440 572 821 1155 1580 1761 2157 2256 2578 2499 2715 2794 3280 3237 3263 3586 3334 3652 3675 4404 4509 5239 6400 9074 11047 13147 15137 13909 6354 1152 183];
% calculate percentages:
pcts = 100*value.'/sum(value);
idx = round(pcts)<7;
combine = number(idx);
pctv = pcts(idx);
nrc = compose('%d (%2d%%)', [number(~idx).', round(pcts(~idx))]);
% VARIED
nrc{end+1} = sprintf('%d %d %d %d %d %d (%2d%%)\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d\n%d %d %d %d %d %d',[combine(1:6) round(sum(pcts(idx))) combine(7:end)]); % NEW 'nrc{end+1}'
cattxt = nrc;
value2 = [value(~idx) sum(value(idx))];
figure
p = pie(value2);
pPatch = findobj(p, 'Type','Patch');
cm = colormap(turbo(numel(pPatch))); % Colour Array (Can Be Whatever You Define It To Be)
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
for k = 1:numel(cattxt)
pPatch(k).FaceColor = cm(k,:); % Colour Each Patch Individually
pText(k).String = []; % Delete The 'String' Labels
end
lgd = legend(cattxt, 'Location','eastoutside','FontSize',12);

Sign in to comment.

More Answers (2)

It is worth checking out the new (as of R2023b) piechart command.
A new feature (added in R2024a) is the LabelStyle property, which can be used to quickly create the labels you were asking about.
x = [1,2,3];
txt = {'Item A';'Item B';'Item C'};
p = piechart(x, txt);
p.LabelStyle = 'namepercent';
The code you provided is mostly correct, but you can simplify it a bit. Here's how you can achieve the same result with your data:
% Your data
number = [78;79;80;81;82;83;84;85;86;87;88]';
value = [4509;5239;6400;9074;11047;13147;15137;13909;6354;1152;183]';
% Create labels for the pie chart
number2 = cellstr(num2str(number)); % Convert numbers to cell array of strings
% Create the pie chart
p = pie(value);
pText = findobj(p,'Type','text');
percentValues = get(pText,'String');
% Combine labels and percentages
combinedtxt = strcat(number2, ': ', percentValues);
% Set the combined text for each slice
for K = 1:numel(value)
pText(K).String = combinedtxt{K};
end

Categories

Products

Release

R2021b

Tags

Community Treasure Hunt

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

Start Hunting!