How do I plot time-stamped data from file?
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I have a bunch of mixed data that I need to graph (see attached file for an example).
importdata seems to bring the data in properly, but I'm having trouble plotting. As you can see in the file, many measurements are taken as a function of time. I need the time to be on the x-axis and the parameters (choose any one) on the y. I wasn't able to find examples of datetime formats using AM and PM designations, and this is obviously too much data to enter manually.
Thanks for the help.
採用された回答
Peter Perkins
2018 年 1 月 9 日
Reading the data in is relatively easy in recent MATLAB, although there's a couple of tricks. I'm using R2017b, earlier versions may need slightly different tweaks.
First, the file is tab delimited, with some blank lines at the top. Easiest thing is to use detectImportOptions and tell it where the variable names are:
>> opts = detectImportOptions('test.txt');
>> opts.VariableNamesLine = 3
opts =
DelimitedTextImportOptions with properties:
Format Properties:
Delimiter: {'\t'}
Whitespace: '\b '
LineEnding: {'\n' '\r' '\r\n'}
CommentStyle: {}
ConsecutiveDelimitersRule: 'split'
LeadingDelimitersRule: 'keep'
EmptyLineRule: 'skip'
Encoding: 'ISO-8859-1'
Replacement Properties:
MissingRule: 'fill'
ImportErrorRule: 'fill'
ExtraColumnsRule: 'addvars'
Variable Import Properties: Set types by name using setvartype
VariableNames: {'Var1', 'Var2', 'Var3' ... and 29 more}
VariableTypes: {'datetime', 'datetime', 'double' ... and 29 more}
SelectedVariableNames: {'Var1', 'Var2', 'Var3' ... and 29 more}
VariableOptions: Show all 32 VariableOptions
Access VariableOptions sub-properties using setvaropts/getvaropts
Location Properties:
DataLine: 5
VariableNamesLine: 3
RowNamesColumn: 0
VariableUnitsLine: 0
VariableDescriptionsLine: 0
Then read the file.
>> t = readtable('test.txt',opts,'ReadVariableNames',true);
Warning: The DATETIME data was created using format 'MM/dd/uuuu' but also matched 'dd/MM/uuuu'.
To avoid ambiguity, supply a datetime format using SETVAROPTS, e.g.
opts = setvaropts(opts,varname,'InputFormat','MM/dd/uuuu');
> In matlab.io.internal.text.TableParser/readData (line 75)
In matlab.io.text.TextImportOptions/readtable (line 223)
>> head(t)
ans =
8×32 table
Date Time Rate1 Rate2 Thick_1 Thick_2 PowerEG1 HighVolt_1 HVPSEm_1 Emission1 Filament1 ISRFPower ISRFReflectedPower ISCoilCurrent ISIgnitorVoltage MFC1Flow MFC2Flow MFC3Flow GasFlowRes_4 Subst_Temp_ HeatPower ChamberTemp_ CryoTemperature1_Stage CryoTemperature2_Stage ChamberP_ ForelineP_ PumplineP_ RawFrequency1 RawFrequency2 ShutterE_Gun1 CustomerParameter Var32
____________________ ____________________ _____ _____ _______ _______ ________ __________ ________ _________ _________ _________ __________________ _____________ ________________ ________ ________ ________ ____________ ___________ _________ ____________ ______________________ ______________________ _________ __________ __________ _____________ _____________ _____________ _________________ _____
01-Dec-2017 00:00:00 09-Jan-2018 11:04:21 0 0 -1992 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.4 5.3e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 09-Jan-2018 11:04:22 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.24 0 27.1 0 0 32.8 6.3 5.28e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 09-Jan-2018 11:04:22 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.2 5.27e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 09-Jan-2018 11:04:23 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.24 0 27.1 0 0 32.8 6.1 5.23e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 09-Jan-2018 11:04:23 0.1 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.24 0 27.1 0 0 32.8 6.1 5.21e-05 '' 1.41e-07 0.301 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 09-Jan-2018 11:04:24 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.2 5.2e-05 '' 1.41e-07 0.303 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 09-Jan-2018 11:04:24 0.1 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.2 5.17e-05 '' 1.41e-07 0.304 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 09-Jan-2018 11:04:25 0 0 0 0 0 747.8 45 45 -0.31 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.1 5.14e-05 '' 1.41e-07 0.305 5.9848e+06 0 0 0
That warning is important, but I'm gonna guess that you file uses MM/dd. You will also notive that Time was read in as a datetime, with the correct time frm the file but with today's date. We can patch that up.
>> t.Time = timeofday(t.Time);
>> head(t)
ans =
8×32 table
Date Time Rate1 Rate2 Thick_1 Thick_2 PowerEG1 HighVolt_1 HVPSEm_1 Emission1 Filament1 ISRFPower ISRFReflectedPower ISCoilCurrent ISIgnitorVoltage MFC1Flow MFC2Flow MFC3Flow GasFlowRes_4 Subst_Temp_ HeatPower ChamberTemp_ CryoTemperature1_Stage CryoTemperature2_Stage ChamberP_ ForelineP_ PumplineP_ RawFrequency1 RawFrequency2 ShutterE_Gun1 CustomerParameter Var32
____________________ ________ _____ _____ _______ _______ ________ __________ ________ _________ _________ _________ __________________ _____________ ________________ ________ ________ ________ ____________ ___________ _________ ____________ ______________________ ______________________ _________ __________ __________ _____________ _____________ _____________ _________________ _____
01-Dec-2017 00:00:00 11:04:21 0 0 -1992 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.4 5.3e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 11:04:22 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.24 0 27.1 0 0 32.8 6.3 5.28e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 11:04:22 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.2 5.27e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 11:04:23 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.24 0 27.1 0 0 32.8 6.1 5.23e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 11:04:23 0.1 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.24 0 27.1 0 0 32.8 6.1 5.21e-05 '' 1.41e-07 0.301 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 11:04:24 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.2 5.2e-05 '' 1.41e-07 0.303 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 11:04:24 0.1 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.2 5.17e-05 '' 1.41e-07 0.304 5.9848e+06 0 0 0
01-Dec-2017 00:00:00 11:04:25 0 0 0 0 0 747.8 45 45 -0.31 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.1 5.14e-05 '' 1.41e-07 0.305 5.9848e+06 0 0 0
It's possible that you want the date and time combined, also easy. Make it a timetable.
>> t.Time = t.Date + t.Time;
>> t.Date = [];
>> tt = table2timetable(t);
>> head(tt)
ans =
8×30 timetable
Time Rate1 Rate2 Thick_1 Thick_2 PowerEG1 HighVolt_1 HVPSEm_1 Emission1 Filament1 ISRFPower ISRFReflectedPower ISCoilCurrent ISIgnitorVoltage MFC1Flow MFC2Flow MFC3Flow GasFlowRes_4 Subst_Temp_ HeatPower ChamberTemp_ CryoTemperature1_Stage CryoTemperature2_Stage ChamberP_ ForelineP_ PumplineP_ RawFrequency1 RawFrequency2 ShutterE_Gun1 CustomerParameter Var32
____________________ _____ _____ _______ _______ ________ __________ ________ _________ _________ _________ __________________ _____________ ________________ ________ ________ ________ ____________ ___________ _________ ____________ ______________________ ______________________ _________ __________ __________ _____________ _____________ _____________ _________________ _____
01-Dec-2017 11:04:21 0 0 -1992 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.4 5.3e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 11:04:22 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.24 0 27.1 0 0 32.8 6.3 5.28e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 11:04:22 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.2 5.27e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 11:04:23 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.24 0 27.1 0 0 32.8 6.1 5.23e-05 '' 1.41e-07 0.3 5.9848e+06 0 0 0
01-Dec-2017 11:04:23 0.1 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.24 0 27.1 0 0 32.8 6.1 5.21e-05 '' 1.41e-07 0.301 5.9848e+06 0 0 0
01-Dec-2017 11:04:24 0 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.2 5.2e-05 '' 1.41e-07 0.303 5.9848e+06 0 0 0
01-Dec-2017 11:04:24 0.1 0 0 0 0 747.8 45 45 -0.32 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.2 5.17e-05 '' 1.41e-07 0.304 5.9848e+06 0 0 0
01-Dec-2017 11:04:25 0 0 0 0 0 747.8 45 45 -0.31 0 0 0 0 0 -0.64 -0.23 0 27.1 0 0 32.8 6.1 5.14e-05 '' 1.41e-07 0.305 5.9848e+06 0 0 0
Now just plot, either one at a time
plot(tt.Time,tt.Rate1)
or several at a time
plot(tt.Time,tt(:,{'Rate1' 'Rate2'}}))
1 件のコメント
Thanks, Peter. This helped a lot. I didn't really know how to deal with this poorly formatted file. I think your answer boiled down to:
opts = detectImportOptions(filename);
opts.VariableNamesLine = 3;
t = readtable(filename,opts,'ReadVariableNames',true);
t.Time = timeofday(t.Time);
followed by some plotting. The ttplot looks really good, too. I'll definitely check that out.
Really appreciate it. Thanks again.
Edit: By the way, everything worked in 2017a.
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Calendar についてさらに検索
参考
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)
