present decimal values in a pie graph

Hello, I'm struggling in a specific action in a pie graph. After I input a vector of numbers, if the sum of the numbers is bigger than 100, than the program should do the following action in a for loop.
afterwards a pie graph should be created and the numbers should be presented in their decimal value. my code works fine and creates the graph besides the decimal part. I would love to get a hand in here how to turn my values into their decimal values (I know this code won't do it, I'm not familiar with any command that would do that).
else sum(num) > 100
for i = 1 ; length(num) %for loop that takes every value, divides it by the sum of the vector and multiply by 100%
num(i) = ((num(i)/sum(num))*100);
end
pie(num);
thanks in advance.

 採用された回答

dpb
dpb 2019 年 5 月 3 日
編集済み: dpb 2019 年 5 月 3 日

1 投票

...
for i = 1 ; length(num) % ";" should be ":"
num(i) = ((num(i)/sum(num))*100);
end
Use Matlab array syntax instead of loops...
num=100*num/sum(num); % Matlab is "MATrix LABoratory", use vectorized operations
pie(num,cellstr(num2str(num(:),'%05.2f%'))) % write percentages as labels
NB: the (:) on the num array to ensure is column vector in argument to num2str to ensure generates a columnar list of char strings; a row vector gets turned into one long string instead, not at all what you want.
"Salt to suit" the precision for the format string...
ADDENDUM/ERRATUM
Fixed missing cellstr() cast and mistyped format string (had 5f.2 rather than 5.2f)

4 件のコメント

Nitai
Nitai 2019 年 5 月 3 日
編集済み: Nitai 2019 年 5 月 3 日
hey DB, thx for your help. when i try to write this:
num=100*num/sum(num); % Matlab is "MATrix LABoratory", use vectorized operations
pie(num,num2str(num(:),'%05f.2')) % write percentages as labels
it won't work and displays the following error:
Error using pie (line 71)
X and EXPLODE must be the same length.
and i didnt completly understand your note at the end of your msg.
any thougths?
dpb
dpb 2019 年 5 月 3 日
Was off the doc; hadn't tried it...needs a cellstr() array, not a cell array of char() it appears...
pie(num,num2str(num(:),'%05.2f%')) % write percentages as labels
Actually, I see that if you're happy with just percentages as the integer values that pie does that automatically; you don't need to write the text at all...if you want more precision, looks like need to supply.
pie(num) % shows percentages as integers by default
As far as the last comment, see difference between
>> x % some sample percentage values
x =
33.33
35.29
3.92
15.69
11.76
>> cellstr(num2str(x,'%05.2f')) % x is column vector, get column of cellstr()
ans =
5×1 cell array
{'33.33'}
{'35.29'}
{'03.92'}
{'15.69'}
{'11.76'}
>> cellstr(num2str(x.','%05.2f')) % if x is row vector (.' does that to column)
ans =
1×1 cell array
{'33.3335.2903.9215.6911.76'}
>>
the last ends up with one long string of the numbers in internal storage order of the string representation of the values of x -- not at all what you want/need...
Giuseppe Degan Di Dieco
Giuseppe Degan Di Dieco 2021 年 7 月 29 日
Dear dpb,
thanks both for your tip and example.
Easy and clear.
Best!
dpb
dpb 2021 年 7 月 29 日
Glad it helped...

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

タグ

質問済み:

2019 年 5 月 3 日

コメント済み:

dpb
2021 年 7 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by