MATLAB: How to plot time-stamped data from file

am/pmdatetimeimportdata

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.

Best Answer

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'}}))
There's also ttplot to make a set of separate plots, stacked.