Read excel file with Matlab
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
Hello,
I have excel file with three columns "Time", "Altitude, And "Shadow length" that I want to read in Matlab. I tried this commands below, however, the column "Time" format change to decimal number. What can I do to keep the time format the same?
Thank you!
T = readtable('Shadedata.xlsx');
opts = detectImportOptions('Shadedata.xlsx');
preview('Shadedata.xlsx',opts)
採用された回答
Star Strider
2023 年 8 月 18 日
One option is to use the datetime 'ConvertFrom' name-value pair. For the ‘value’ argument, both 'excel' and 'posixtime' work, with 'excel' appearing to be correct (in that it gives 15-minute intervals) —
% F = fileread('Shadedata.xlsx')
% C = readcell('Shadedata.xlsx')
T = readtable('Shadedata.xlsx', 'VAriableNamingRule','preserve')
T = 40×3 table
Time Altitude [degree celcius] Shadow length [m]
_______ _________________________ _________________
0.32292 1.34 2135.8
0.33333 2.34 808.83
0.34375 3.34 495.25
0.35417 4.34 357.42
0.36458 5.34 280.44
0.375 6.34 231.5
0.38542 7.34 197.77
0.39583 8.34 173.22
0.40625 17.92 154.65
0.41667 19.63 140.22
0.42708 21.22 128.79
0.4375 22.68 119.62
0.44792 24.02 112.22
0.45833 25.2 106.24
0.46875 26.24 101.45
0.47917 27.11 97.68
T1 = T;
T1.Time = datetime(T1.Time, 'ConvertFrom','excel')
T1 = 40×3 table
Time Altitude [degree celcius] Shadow length [m]
____________________ _________________________ _________________
31-Dec-1899 07:45:00 1.34 2135.8
31-Dec-1899 08:00:00 2.34 808.83
31-Dec-1899 08:15:00 3.34 495.25
31-Dec-1899 08:30:00 4.34 357.42
31-Dec-1899 08:45:00 5.34 280.44
31-Dec-1899 09:00:00 6.34 231.5
31-Dec-1899 09:15:00 7.34 197.77
31-Dec-1899 09:30:00 8.34 173.22
31-Dec-1899 09:45:00 17.92 154.65
31-Dec-1899 10:00:00 19.63 140.22
31-Dec-1899 10:15:00 21.22 128.79
31-Dec-1899 10:30:00 22.68 119.62
31-Dec-1899 10:45:00 24.02 112.22
31-Dec-1899 11:00:00 25.2 106.24
31-Dec-1899 11:15:00 26.24 101.45
31-Dec-1899 11:30:00 27.11 97.68
T2 = T;
T2.Time = datetime(T2.Time, 'ConvertFrom','posixtime')
T2 = 40×3 table
Time Altitude [degree celcius] Shadow length [m]
____________________ _________________________ _________________
01-Jan-1970 00:00:00 1.34 2135.8
01-Jan-1970 00:00:00 2.34 808.83
01-Jan-1970 00:00:00 3.34 495.25
01-Jan-1970 00:00:00 4.34 357.42
01-Jan-1970 00:00:00 5.34 280.44
01-Jan-1970 00:00:00 6.34 231.5
01-Jan-1970 00:00:00 7.34 197.77
01-Jan-1970 00:00:00 8.34 173.22
01-Jan-1970 00:00:00 17.92 154.65
01-Jan-1970 00:00:00 19.63 140.22
01-Jan-1970 00:00:00 21.22 128.79
01-Jan-1970 00:00:00 22.68 119.62
01-Jan-1970 00:00:00 24.02 112.22
01-Jan-1970 00:00:00 25.2 106.24
01-Jan-1970 00:00:00 26.24 101.45
01-Jan-1970 00:00:00 27.11 97.68
Choose the result that makes the best sense.
.
6 件のコメント
Sanley Guerrier
2023 年 8 月 18 日
Looks great, Thank you!
my date is 10-Feb-2023, how do I update your code to have someting like 10-Feb-2023 07:45:00 ?
Les Beckham
2023 年 8 月 18 日
One possible way to offset the data to that desired date is this:
T = readtable('Shadedata.xlsx', 'VariableNamingRule', 'preserve');
T1 = T;
offset = exceltime(datetime(2023, 2, 10));
T1.Time = T1.Time + offset;
T1.Time = datetime(T1.Time, 'ConvertFrom', 'excel')
T1 = 40×3 table
Time Altitude [degree celcius] Shadow length [m]
____________________ _________________________ _________________
10-Feb-2023 07:45:00 1.34 2135.8
10-Feb-2023 08:00:00 2.34 808.83
10-Feb-2023 08:15:00 3.34 495.25
10-Feb-2023 08:30:00 4.34 357.42
10-Feb-2023 08:45:00 5.34 280.44
10-Feb-2023 09:00:00 6.34 231.5
10-Feb-2023 09:15:00 7.34 197.77
10-Feb-2023 09:30:00 8.34 173.22
10-Feb-2023 09:45:00 17.92 154.65
10-Feb-2023 10:00:00 19.63 140.22
10-Feb-2023 10:15:00 21.22 128.79
10-Feb-2023 10:30:00 22.68 119.62
10-Feb-2023 10:45:00 24.02 112.22
10-Feb-2023 11:00:00 25.2 106.24
10-Feb-2023 11:15:00 26.24 101.45
10-Feb-2023 11:30:00 27.11 97.68
Sanley Guerrier
2023 年 8 月 18 日
Perfect, thanks a lot!
Star Strider
2023 年 8 月 18 日
As always, my pleasure!
I don’t see anything that directly addresses that problem ('PivotYear' would only change the year) and dateshift doesn’t appear to provide the necessary options.
So, I improvised —
T = readtable('Shadedata.xlsx', 'VAriableNamingRule','preserve')
T = 40×3 table
Time Altitude [degree celcius] Shadow length [m]
_______ _________________________ _________________
0.32292 1.34 2135.8
0.33333 2.34 808.83
0.34375 3.34 495.25
0.35417 4.34 357.42
0.36458 5.34 280.44
0.375 6.34 231.5
0.38542 7.34 197.77
0.39583 8.34 173.22
0.40625 17.92 154.65
0.41667 19.63 140.22
0.42708 21.22 128.79
0.4375 22.68 119.62
0.44792 24.02 112.22
0.45833 25.2 106.24
0.46875 26.24 101.45
0.47917 27.11 97.68
T.Time = datetime(T.Time, 'ConvertFrom','excel')
T = 40×3 table
Time Altitude [degree celcius] Shadow length [m]
____________________ _________________________ _________________
31-Dec-1899 07:45:00 1.34 2135.8
31-Dec-1899 08:00:00 2.34 808.83
31-Dec-1899 08:15:00 3.34 495.25
31-Dec-1899 08:30:00 4.34 357.42
31-Dec-1899 08:45:00 5.34 280.44
31-Dec-1899 09:00:00 6.34 231.5
31-Dec-1899 09:15:00 7.34 197.77
31-Dec-1899 09:30:00 8.34 173.22
31-Dec-1899 09:45:00 17.92 154.65
31-Dec-1899 10:00:00 19.63 140.22
31-Dec-1899 10:15:00 21.22 128.79
31-Dec-1899 10:30:00 22.68 119.62
31-Dec-1899 10:45:00 24.02 112.22
31-Dec-1899 11:00:00 25.2 106.24
31-Dec-1899 11:15:00 26.24 101.45
31-Dec-1899 11:30:00 27.11 97.68
[h,m,s] = hms(T.Time); % Get Time Data
T.Time = datetime([repmat([2023 2 10],numel(h),1) h m s]) % Create New Dates & Times
T = 40×3 table
Time Altitude [degree celcius] Shadow length [m]
____________________ _________________________ _________________
10-Feb-2023 07:45:00 1.34 2135.8
10-Feb-2023 08:00:00 2.34 808.83
10-Feb-2023 08:15:00 3.34 495.25
10-Feb-2023 08:30:00 4.34 357.42
10-Feb-2023 08:45:00 5.34 280.44
10-Feb-2023 09:00:00 6.34 231.5
10-Feb-2023 09:15:00 7.34 197.77
10-Feb-2023 09:30:00 8.34 173.22
10-Feb-2023 09:45:00 17.92 154.65
10-Feb-2023 10:00:00 19.63 140.22
10-Feb-2023 10:15:00 21.22 128.79
10-Feb-2023 10:30:00 22.68 119.62
10-Feb-2023 10:45:00 24.02 112.22
10-Feb-2023 11:00:00 25.2 106.24
10-Feb-2023 11:15:00 26.24 101.45
10-Feb-2023 11:30:00 27.11 97.68
DI = cumsum([0; diff(hour(T.Time))<0]); % Check For Midnight Rollover
T.Time = T.Time + days(DI) % Increment Days For Midnight Rollovers
T = 40×3 table
Time Altitude [degree celcius] Shadow length [m]
____________________ _________________________ _________________
10-Feb-2023 07:45:00 1.34 2135.8
10-Feb-2023 08:00:00 2.34 808.83
10-Feb-2023 08:15:00 3.34 495.25
10-Feb-2023 08:30:00 4.34 357.42
10-Feb-2023 08:45:00 5.34 280.44
10-Feb-2023 09:00:00 6.34 231.5
10-Feb-2023 09:15:00 7.34 197.77
10-Feb-2023 09:30:00 8.34 173.22
10-Feb-2023 09:45:00 17.92 154.65
10-Feb-2023 10:00:00 19.63 140.22
10-Feb-2023 10:15:00 21.22 128.79
10-Feb-2023 10:30:00 22.68 119.62
10-Feb-2023 10:45:00 24.02 112.22
10-Feb-2023 11:00:00 25.2 106.24
10-Feb-2023 11:15:00 26.24 101.45
10-Feb-2023 11:30:00 27.11 97.68
All the times are the same day here, however I included code that will increment the days in the event other data sets would have a midnight rollover. (I have used this approach with other problems.)
.
Sanley Guerrier
2023 年 8 月 18 日
Thanks a lot!
Star Strider
2023 年 8 月 18 日
As always, my pleasure!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Spreadsheets についてさらに検索
タグ
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
