Qiita Conference 2025

ymrl (@ymrl)

がんばらないアクセシビリティ: より幅広く価値を届けるために

12
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

MATLABでコーンフレークの栄養グラフっぽいのを描こう。

Posted at

コーンフレークの栄養グラフが気になる。

 ミルクボーイ面白かったですね!ということで、MATLAB でも栄養バランスの五角形みたいなグラフを書いてみましょう。

前にスタバのフード情報を取ってきたので、使いまわそう。

 ちょうどよく前の記事でスタバの栄養素が取ってあったので、これをグラフにしてみましょう。グラフにするので、1日に必要な栄養素にまで正規化しておきましょう。

%% ウェブから落としてくる。
url = "https://www.starbucks.co.jp/assets/images/web2/images/allergy/pdf/allergen-food.pdf";
websave('starbucks_food.pdf',url)

%% 関連しそうな文字列をごっそり取り出す。
D = extractFileText('starbucks_food.pdf');
D = extractBetween(D,"食塩相当量(g)","お問い合わせ先");
D = replace(D,newline,' ');
X = regexp(D,'[^0-9]+ [0-9.]+ [0-9.]+ [0-9.]+ [0-9.]+ [0-9.]+','match');

%% テーブルを作ったり。
for n = 1:length(X)
    spl = split(X(n));
    Name(n,1) = join(spl(1:end-5));
    Energy(n,1) = double(spl(end-4));
    Protein(n,1) = double(spl(end-3));
    Fat(n,1) = double(spl(end-2));
    Carbo(n,1) = double(spl(end-1));
    Salt(n,1) = double(spl(end));
end

T = table(Name,Energy,Protein,Fat,Carbo,Salt);

%% 一日に必要な栄養素で正規化しましょう。
T_full = [2600  60  500  250  8];  % だいたいこのくらいらしい。(kcal, g, g, g, g)
T{:,2:end} = T{:,2:end}./T_full;

 どんなもんかね。コマンドウィンドウで見てみよう。

>> T(1:3,:)

ans =

  3×6 table

                     Name                     Energy     Protein      Fat      Carbo      Salt 
    ______________________________________    _______    ________    ______    ______    ______

    " ペストリー ブルーベリースコーン"             0.11077    0.091667    0.0222    0.1692       0.1
    " ペストリー ストロベリーチーズケーキスコーン"    0.13846     0.12667    0.0306     0.196      0.15
    " ペストリー チョコレートチャンクスコーン"       0.13577        0.11     0.032    0.1848    0.1375

よさそう!

テーブルを入力として、2列目以降をプロットする関数を作ろう。

 1列目が名前で、2列目以降が栄養素としてプロットできるようにしましょう。sin / cos を使って、こんな感じの関数を作ればいいね。

corn_plot.m
function corn_plot(T,color)
[m,n] = size(T);
for ix = 0:n-2
    plot(linspace(0,sind(ix/(n-1)*360),5),linspace(0,cosd(ix/(n-1)*360),5),'+-')
    text(sind(ix/(n-1)*360), cosd(ix/(n-1)*360), T.Properties.VariableNames{ix+2})
    hold on
end
for ix = 1:m
    x = T{ix,2:end} .* sind((0:n-2)/(n-1)*360);
    y = T{ix,2:end} .* cosd((0:n-2)/(n-1)*360);
    P = patch(x,y,color);
    P.FaceAlpha = 0.2;
    P.EdgeColor = 'r';
    P.LineWidth = 3;
end
axis equal
axis off
title([T{ix,1},'は、栄養バランス満点!!'])
end

 じゃあブルーベリースコーンでもプロットしてみましょう!コマンドウィンドウでこんな感じ。

>> corn_plot(T(1,:),'b')

blueberry.png

全然、栄養バランス満点に見えない!
値が小さいから、3つくらい重ねて描いてみよう。

>> corn_plot(T(1,:),'b')
>> corn_plot(T(2,:),'g')
>> corn_plot(T(3,:),'c')

scone.png

・・・きっとスコーンはダイエットにいいのね。

全部重ねてみよう。

>> corn_plot(T,'c')

starbucks_all.png

圧倒的な大きさのものがないと見栄えしない。。

とりあえずできた。

 ビタミンとかの指標がないと見栄えが悪いということと、コーンフレークのグラフの大きさはすごいな、ということでいいか。。

12
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
12
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?