入出館のデータから滞在人数のグラフを作る方法

添付csvの入出館のデータから、滞在人数の線グラフ(横軸10分単位の時間、縦軸滞在人数)を作ることはできるでしょうか?
1日悩んだのですが、どうにも判りません。(もしかしたら出来ないのかも)
よろしくお願いいたします。

 Accepted Answer

Atsushi Ueno
Atsushi Ueno on 1 Jun 2023

1 vote

isbetween 関数を使えば、日付と時刻の区間内の要素を判別出来ます。
data = readtable('https://jp.mathworks.com/matlabcentral/answers/uploaded_files/1399879/Book1.csv');
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
for k = 1:height(data) % 入退室それぞれの日付と時刻をくっ付ける(もっと上手いやり方があるはず)
data2(k,1) = datetime(strcat(string(data{k,1})," ",string(data{k,2})),'InputFormat','yyyy/MM/dd HH:mm:ss');
data2(k,2) = datetime(strcat(string(data{k,3})," ",string(data{k,4})),'InputFormat','yyyy/MM/dd HH:mm:ss');
end
number = [];
for tm = min(data2(:)):minutes(10):max(data2(:)) % 10分おきに何人居るか数える
number = [number sum(isbetween(tm,data2(:,1),data2(:,2)))];
end
stairs(min(data2(:)):minutes(10):max(data2(:)),number); % 階段状の線プロット

3 Comments

Kazu Ari
Kazu Ari on 1 Jun 2023
Moved: Atsushi Ueno on 2 Jun 2023
ありがとうございます。
多分無理だろう、と思っていた事が出来るとは。
関数があることも、それを使うアイデアも素晴らしいです。
プログラムを見直し for 文を除去等、上手いやり方にしました。MATLABで for 文使ったら負け🤔
data = readtable('https://jp.mathworks.com/matlabcentral/answers/uploaded_files/1399879/Book1.csv');
data2 = datetime(data{:,[1 3]},'InputFormat','yyyy/MM/dd') + data{:,[2 4]}; % 入退室それぞれの日付と時刻をくっ付ける
tmslc = min(data2(:)):minutes(10):max(data2(:)); % 10分おきのタイムスライス
number = sum(isbetween(tmslc,data2(:,1),data2(:,2))); % 何人居るか数える
stairs(tmslc,number); % 階段状の線プロット
Kazu Ari
Kazu Ari on 6 Jun 2023
有難うございます。
「MATLABでfor文をつかったら負け」
これが気になってしまい、他で作成中のスクリプトの見直しに入ってしまいました。
VBAから始めたのでどうしてもfor文を使ってしまうんですよね。

Sign in to comment.

More Answers (0)

Products

Release

R2021b

Community Treasure Hunt

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

Start Hunting!