how to solve the Datenum failed error

8 ビュー (過去 30 日間)
Rashni Anandawansha
Rashni Anandawansha 2020 年 10 月 23 日
コメント済み: Peter Perkins 2020 年 11 月 20 日
I've been trying to run a matlab code which uses datenum ,
fid=fopen('./stationeventinfo_1.dat','r');
tab_evt=textscan(fid,'%f%f%f%f%f%f');
fclose(fid);
n_d=length(tab_evt{1});
for ie=1:n_d
data_yr(ie)=tab_evt(ie,1);
data_mo(ie)=tab_evt(ie,2);
data_dy(ie)=tab_evt(ie,3);
data_hr(ie)=tab_evt(ie,4);
data_min(ie)=tab_evt(ie,5);
data_sec(ie)=tab_evt(ie,6);
data_time=datenum(data_yr(ie),data_mo(ie),data_dy(ie),...
data_hr(ie),data_min(ie),data_sec(ie));
end
where file stationeventinfo_1.dat is given as (i have more input line like this in this file)
1998 09 28 14 07 44
I keep getting this error which i cannot figure out how to resolve,
Error using datenum (line 190)
DATENUM failed.
Error in datecheck (line 24)
data_time=datenum(data_yr(ie),data_mo(ie),data_dy(ie),...
Caused by:
Error using datenummx
The datenummx function only accepts double arrays.
Please help :)

採用された回答

Walter Roberson
Walter Roberson 2020 年 10 月 24 日
編集済み: Walter Roberson 2020 年 10 月 24 日
textscan() returns a cell array with one entry per column. Using () indexing to a cell array will get you a cell array in return.
data_yr(ie)=tab_evt{1}(ie);
Note: you are overwriting all of data_time every iteration of your loop.
You do not need any loop there are at all.
data_time = datenum(cell2mat(tab_evt));
We do not recommend datenum these days; datetime objects are typically much more convenient.
  2 件のコメント
Rashni Anandawansha
Rashni Anandawansha 2020 年 10 月 26 日
Thank you :)
Peter Perkins
Peter Perkins 2020 年 11 月 20 日
Strongly recommend you heed Walter's advice about using datetime; I would also add that you should use readtable or readmatrix and not textscan.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDates and Time についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by