extract data from table matlab
Show older comments

for a job I have to extract information from this table, in particular I want to have a new table with only the information regarding T001, T002, T003, T004 (third coloumn) there is a quick way to do this on matLab. Thanks I have recently used it
S=readtable('paris.txt')
day=S(:,1)
time=S(:,2)
sensor=S(:,3)
sensor_out=S(:,4)
paris_table=table(day,time,sensor,sensor_out)
This is what i do
Accepted Answer
More Answers (2)
That's messy...that they didn't put the data into separate columns makes it rougher...almost do have to separate out the T sensors from the M whatever-they-ares to do anything useful. Could separate into another column; for demo here since it's only T you show above I just split them out...
Here's about how I'd go about it with that input file as starting point:
opt=detectImportOptions('milin.txt','ReadVariableNames',0); % first get an import object
opt.VariableNames={'Date','Sensor','Value'}; % set variable names for those wanted
opt.SelectedVariableNames=opt.VariableNames; % and read only them
opt.ExtraColumnsRule='ignore'; % and don't add other variables
M=readtable('milin.txt',opt); % now read the table
The above gives us
>> head(M)
>> [head(M);tail(M)]
ans =
16×3 table
Date Sensor Value
____________________ ________ _________
16-Oct-0009 00:01:00 {'M017'} {'ON' }
16-Oct-0009 00:01:00 {'M009'} {'ON' }
16-Oct-0009 00:01:00 {'M017'} {'OFF' }
16-Oct-0009 00:01:00 {'M019'} {'ON' }
16-Oct-0009 00:01:00 {'M009'} {'OFF' }
16-Oct-0009 00:01:00 {'M019'} {'OFF' }
16-Oct-0009 00:08:00 {'M020'} {'ON' }
16-Oct-0009 00:08:00 {'M020'} {'OFF' }
06-Jan-0010 12:48:00 {'D001'} {'CLOSE'}
06-Jan-0010 12:48:00 {'M001'} {'OFF' }
06-Jan-0010 12:48:00 {'M003'} {'ON' }
06-Jan-0010 12:48:00 {'M002'} {'OFF' }
06-Jan-0010 12:48:00 {'M027'} {'OFF' }
06-Jan-0010 12:48:00 {'M027'} {'ON' }
06-Jan-0010 12:48:00 {'M003'} {'OFF' }
06-Jan-0010 12:48:00 {'M003'} {'ON' }
>>
as starting point. NB: the import function recognized the datetime first column altho I did have to fix the display format in Excel that seemed to only be time. That's up to you what detail you need/don't...
M=M(contains(M.Sensor,'T'),:); % save only the 'T' sensors for now
M.Sensor=categorical(M.Sensor); % turn sensor into categorical variable
M.Value=str2double(M.Value); % and data into numeric
meanBySensor=rowfun(@mean,M,'InputVariables',{'Value'}, ...
'GroupingVariables',{'Sensor'}, ...
'OutputVariableNames','SensorMeans');
is the example of operating by sensor globally...
>> format bank,format compact
>> meanBySensor
meanBySensor =
2×3 table
Sensor GroupCount SensorMeans
______ __________ ___________
T001 6734.00 22.25
T002 5539.00 21.09
>>
gives the above table.
To illustrate multiple variables grouping, lets do by DOW...
[DOW,WKDY]=weekday(M.Date); % first get the ordinal dow, weekday name for table
[~,wkdays]=weekday(1:7); % generate weekday names for a week
wkdays=circshift(string(wkdays),-1); % put in sequential order begin Sunday
M.DOW=categorical(string(WKDY),wkdays); % and add to the table
meanBySensor_DOW=rowfun(@mean,M,'InputVariables',{'Value'}, ...
'GroupingVariables',{'Sensor','DOW'}, ...
'OutputVariableNames','SensorMeans');
gives:
>> meanBySensor_DOW
meanBySensor_DOW =
14×4 table
Sensor DOW GroupCount SensorMeans
______ ___ __________ ___________
T001 Sun 927.00 22.58
T001 Mon 978.00 22.50
T001 Tue 986.00 21.81
T001 Wed 1070.00 22.01
T001 Thu 1053.00 22.62
T001 Fri 852.00 22.17
T001 Sat 868.00 22.03
T002 Sun 753.00 21.43
T002 Mon 797.00 21.34
T002 Tue 788.00 20.71
T002 Wed 867.00 20.83
T002 Thu 884.00 21.39
T002 Fri 736.00 21.04
T002 Sat 714.00 20.91
>>
9 Comments
Emanuele De Astis
on 30 Jan 2021
dpb
on 30 Jan 2021
NB: A correction -- had deleted one line needed to generate the wkdays array from weekday
dpb
on 30 Jan 2021
If that does get you going, please ACCEPT the Answer to let others know is a solution if for no other reason...
Emanuele De Astis
on 30 Jan 2021
Emanuele De Astis
on 30 Jan 2021
Oh, yeah...I had created a DOW column previously to test in the workspace and didn't catch when copied to forum. It's not yet available when first building the categorical variable. Use
M.DOW=categorical(string(WKDY),wkdays);
instead -- I made the edit above.
I just showed head(M) or [head(M);tail(M)] to only look at a manageable amount of the whole table -- it's
>> height(M)
ans =
12273
>>
rows long; I didn't much want to look at all of that....just a little to see was doing what wanted it to.
Have to see what you did to write the .csv file; writetable should work just fine.
>> writetable(head(M),'testmilin.csv')
>> type testmilin.csv
Date,Sensor,Value,DOW
16-Oct-0009 01:12:00,T001,20.5,Fri
16-Oct-0009 01:27:00,T002,20.5,Fri
16-Oct-0009 01:32:00,T002,21,Fri
16-Oct-0009 01:37:00,T002,20.5,Fri
16-Oct-0009 04:38:00,T002,20,Fri
16-Oct-0009 04:48:00,T001,20,Fri
16-Oct-0009 07:49:00,T002,19.5,Fri
16-Oct-0009 07:54:00,T002,20,Fri
>>
works here; again NB: I just did head(M) so the file wouldn't be huge; use
writetable(M,'newmilan.csv','QuoteStrings',1)
for the whole thing. This will also put the date-time into a quoted string so when reading the file the date and time will be interpreted as one field as should be instead of two.
>> type testmilan.csv
Date,Sensor,Value,DOW
"16-Oct-0009 01:12:00","T001",20.5,"Fri"
"16-Oct-0009 01:27:00","T002",20.5,"Fri"
"16-Oct-0009 01:32:00","T002",21,"Fri"
"16-Oct-0009 01:37:00","T002",20.5,"Fri"
"16-Oct-0009 04:38:00","T002",20,"Fri"
"16-Oct-0009 04:48:00","T001",20,"Fri"
"16-Oct-0009 07:49:00","T002",19.5,"Fri"
"16-Oct-0009 07:54:00","T002",20,"Fri"
>>
Emanuele De Astis
on 30 Jan 2021
dpb
on 30 Jan 2021
No problem, but again,
"If that does get you going, please ACCEPT the Answer to let others know is a solution if for no other reason..."
Emanuele De Astis
on 30 Jan 2021
Emanuele De Astis
on 31 Jan 2021
5 Comments
dpb
on 31 Jan 2021
[~,wkdays]=weekday(1:7); % generate weekday names for a week
Emanuele De Astis
on 31 Jan 2021
dpb
on 31 Jan 2021
No
Emanuele De Astis
on 31 Jan 2021
Oh...I had forgotten about the problem in that data file.
One of the comments to the above script I wrote when first posted it was--
". NB: the import function recognized the datetime first column altho I did have to fix the display format in Excel that seemed to only be time. That's up to you what detail you need/don't..."
If there is no date in the time field it will barf, indeed. That's a problem with the input file can't fix in MATLAB; only by correcting the input file to also show the date besides the time.
Categories
Find more on Data Type Identification in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!