![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/719889/image.png)
csvインポート時に、日付をシリアル値でインポートしたい
10 ビュー (過去 30 日間)
古いコメントを表示
csvをインポートする際に、日付の列のみをシリアル値で読み込みたいのですが、方法が分かりませんでした。
行列、テーブル双方の場合でも可能な場合、それぞれの方法をご教示頂けますと嬉しいです。
現状思いつくのですと、以下のような手順になり、もっと簡単な方法がないかと思っています。
①csvをtableでインポート(日付はdatetime指定)
②日付をdatenumでシリアル値に変換
③日付以外のデータと再度合体
0 件のコメント
採用された回答
Hernia Baby
2021 年 8 月 24 日
楽したいのでアプリを活用します
以下のコードはインポート機能を使ってそれをライブスクリプト化したものです
MATLABのフォルダーからBook4.csvをダブルクリックしてインポートアプリを立ち上げてください
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/719889/image.png)
ライブスクリプトの生成で以下のコードが生成されます
opts = delimitedTextImportOptions("NumVariables", 6, "Encoding", "UTF-8");
% 範囲と区切り記号の指定
opts.DataLines = [2, Inf];
opts.Delimiter = ",";
% 列名と型の指定
opts.VariableNames = ["Ping_index", "Distance_gps", "Distance_vl", "Ping_date", "Ping_time", "Ping_milliseconds"];
opts.VariableTypes = ["double", "double", "double", "datetime", "datetime", "double"];
% ファイル レベルのプロパティを指定
opts.ExtraColumnsRule = "ignore";
opts.EmptyLineRule = "read";
% 変数プロパティを指定
opts = setvaropts(opts, "Ping_date", "InputFormat", "yyyy/MM/dd");
opts = setvaropts(opts, "Ping_time", "InputFormat", "HH:mm:ss");
% データのインポート
Book4 = readtable("Book4.csv", opts);
% 一時変数のクリア
clear opts
ここでシリアル値を付け加えます(コードを書いたのはここだけ)
Book4.Ping_date_S = datenum(Book4.Ping_date);
Book4
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で スプレッドシート についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!