How to plot from table

Hello! I already got an answer how to add time column to existing textfile with measurement data in it.
DATA.TXT
1,6
5,7
1,7
5,3
I know what time the measurement started and the measurement frequency but not the ending time. So used these commands:
datetime.setDefaultFormats('default','dd-MM-yyyy HH:mm:ss:SS')
M = dlmread('DATA.TXT');
T_out = timetable(datetime(2017,1,1,13,0,.25*(0:size(M,1)-1)'),M);
T = timetable2table(T_out)
T.Properties.VariableNames = {'Date_Time' 'Measured_value'}
writetable(T,'DATA_TIME.TXT','Delimiter',';');
And the result is DATA_TIME.TXT
Date_Time;Measured_value_1;Measured_value_2
01-01-2017 13:00:00:00;1;6
01-01-2017 13:00:00:25;5;7
01-01-2017 13:00:00:50;1;7
01-01-2017 13:00:00:75;5;3
I would like to plot this data against time (Date vs Measured_value_1; Date vs Measured_value_2). And is it possible to change the time axis to just DAYS in case I have measurement data for one year? How to accomplish this?

 Accepted Answer

dpb
dpb on 2 Sep 2017
Going to a lot of trouble that doesn't seem needed in writing stuff out to files instead of just using the in-memory data directly...
datetime.setDefaultFormats('default','dd-MM-yyyy HH:mm:ss:SS')
M = dlmread('DATA.TXT');
T=datetime(2017,1,1,13,0,.25*(0:size(M,1)-1)'); % build the time vector
T=table(T,M); % put into table
T.Properties.VariableNames = {'Time' 'MV'} % some concise variable names
plot(T.Time,T.MV) % plot the data values vs time
As for the latter question of longer times; you can modify the axes at will; if you plot a long data series plot will automagically fixup ticks/ticklabels to fit the space so you may need to do nothing.

4 Comments

-OP Answer moved to Comment -- dpb
Thank you! One more thing - how to separate data values from imported text file?
The table at the moment is following:
Time MV
______________________ ______
01-01-2017 13:00:00:00 1 6
01-01-2017 13:00:00:25 5 7
01-01-2017 13:00:00:50 1 7
01-01-2017 13:00:00:75 5 3
But I would like it to be:
Time M V
______________________ ___ ___
01-01-2017 13:00:00:00 1 6
01-01-2017 13:00:00:25 5 7
01-01-2017 13:00:00:50 1 7
01-01-2017 13:00:00:75 5 3
So I could plot both data columns separately:
plot(T.Time,T.V)
plot(T.Time,T.M)
dpb
dpb on 2 Sep 2017
Edited: dpb on 2 Sep 2017
T=table(T,M(:,1),M(:,2),'VariableNames',{'Time','M','V'});
While it may sound a little snarky perhaps it's not intended as such--all these questions are answered in the documentation with examples...it's much quicker to use it than to wait on someone here to answer every little detail. We're glad to help, but simple stuff is, well, pretty simple to figure out on own with the use of the examples TMW has provided. Granted, it may take reading through the doc fairly thoroughly to get to the specific section you need, but learning to use it effectively is part of learning Matlab and will pay off many times over on progressing more rapidly than the time invested itself.
Note the other way to do the plot you requested also is to simply refer to the array with indexing --
plot(T.Time,T.MV(:,1))
plot(T.Time,T.MV(:,2))
dpb
dpb on 3 Sep 2017
--{Again moved to Comment -- use the comments for follow-up; don't post Answer except as a real answer to a real question to help keep the forum cleaned up) -- dpb
Thank you! Yesterday I took a Matlab manual from our school library.
dpb
dpb on 3 Sep 2017
Don't you have the help installed locally or have access to the internet online version?

Sign in to comment.

More Answers (0)

Categories

Find more on Creating, Deleting, and Querying Graphics Objects in Help Center and File Exchange

Asked:

on 2 Sep 2017

Commented:

dpb
on 3 Sep 2017

Community Treasure Hunt

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

Start Hunting!