Struggling to plot the data over timestamps!

8 views (last 30 days)
muhammad choudhry
muhammad choudhry on 7 Jan 2022
Commented: Jon on 7 Jan 2022
Hi,
I have data capture with timestamps, I am plotting the data over time stamps, can anyone help as I am struggling to plot it. I pasted the data down below as I reached my limit of sharing files.
Code:
files = ['F:\3-PIV_Experimental_Data\Outlet_110\Data_LaserSheet_D\Data_Head\Data_Raw\Head_Timestamps.xlsx'];
a = readtable(files);
x = a{:,1};
y = a{:,2};
figure()
plot(x,y)
Error:
Error using plot
Invalid data argument.
Error in TimeStamps (line 8)
plot(x,y)
Data:
TimeStamps Head(mm)
13:31:39 0
13:31:49 8.1
13:31:59 15.3
13:32:09 24.4
13:32:19 30.8
13:32:29 38.3
13:32:39 44
13:32:49 50.4
13:32:59 57.6
13:33:09 63.4
13:33:19 68.7
13:33:29 74.1
13:33:39 78.9
13:33:49 83.5
13:33:59 88.3
13:34:09 93.1
13:34:19 97.4
13:34:29 102.1
13:34:39 105.7
13:34:49 109.6
13:34:59 113.6
13:35:09 117
13:35:19 120.3
13:35:29 123.5
13:35:39 127
13:35:49 130.2
13:35:59 133.3
13:36:09 136.2
13:36:19 139.3
13:36:29 142.4
13:36:39 145.5
13:36:49 147.8
13:36:59 150.5
13:37:09 153.4
13:37:19 156
13:37:29 158.7
13:37:39 161
13:37:49 163.4
13:37:59 166
13:38:09 168.5
13:38:19 171.5
13:38:29 174
13:38:39 176.8
13:38:49 179
13:38:59 181.8
13:39:09 184.3
13:39:19 186.9
13:39:29 189.5
13:39:40 192.6
13:39:50 195.2
13:40:00 197.9
13:40:10 200.8
13:40:20 203.1
13:40:30 206
13:40:40 209.4
13:40:50 212
13:41:00 215.4
13:41:10 217.8
13:41:20 221.4
13:41:30 224.4
13:41:40 227.3
13:41:50 230.2
13:42:00 232.8
13:42:10 235.9
13:42:20 238.8
13:42:30 242
13:42:40 244.1
13:42:50 247.2
13:43:00 250.6
13:43:10 253.7
13:43:20 257
13:43:30 259.6
13:43:40 262.8
13:43:50 265.8
13:44:00 269.2
13:44:10 272.4
13:44:20 275.7
13:44:30 278.5
13:44:40 281.6
13:44:50 284.8
13:45:00 288.3
13:45:10 291.5
13:45:20 294.6
13:45:30 297.5
13:45:40 300.8
13:45:50 304.2
13:46:00 307.1
13:46:10 309.9
13:46:20 314
13:46:30 316.9
13:46:40 320.5
13:46:50 323.8
13:47:00 327.1
13:47:10 330.5
13:47:20 334.1
13:47:30 337.7
13:47:40 340.6
13:47:50 344.2
13:48:00 347.3
13:48:10 350.9
13:48:20 354.2
13:48:30 357.6
13:48:40 361.3
13:48:50 364.5
13:49:00 367.9
13:49:10 371.2
13:49:20 374.5
13:49:30 378.1
13:49:40 381
13:49:50 384.1
13:50:00 387.5
13:50:10 390.8
13:50:20 394.1
13:50:30 397
13:50:40 399.9
13:50:50 403.2
13:51:00 406.3
13:51:10 409.3
13:51:20 412.4
13:51:30 415.5
13:51:40 418.6
13:51:50 421.5
13:52:00 424.6
13:52:10 428.4
13:52:20 431.5
13:52:30 434.1
13:52:40 437
13:52:50 440.1
13:53:00 443
13:53:10 445.6
13:53:20 448.3
13:53:30 451.1
13:53:40 454.2
13:53:50 456.8
13:54:00 459.8
13:54:10 462.6
13:54:20 465.2
13:54:30 468.3
13:54:40 470.8
13:54:50 473.6
13:55:00 476.2
13:55:10 478.9
13:55:20 481.7
13:55:30 484.4
13:55:40 487.2
13:55:50 489.7
13:56:00 492.3
13:56:10 495.1
13:56:20 497.8
13:56:30 500
13:56:40 502.4
13:56:50 502.6
  1 Comment
Jon
Jon on 7 Jan 2022
Does the script I provided along with the Excel file I attached run correctly for you? If so then you have to look at what other differences there may be between my Excel file and yours. I am running R2021b.
I see now @Star Strider has also given you some good advice.
Alternatively, reading in from the Excel sheet, just using readtable, as you have done in your origina post gives you serial date number values. You can convert these to datetime array values and plot as follows
files = ['Head_Timestamps.xlsx'];
a = readtable(files);
x = a{:,1};
y = a{:,2};
% convert timestamp numerical values to datetime array
t = datetime(x,'ConvertFrom','datenum');
figure
plot(t,y)
which gives the following (MATLAB doesn't know what month or year is plotted so it gives Dec31,-1) if this bothers you, it could probably be cleaned up with a little more work

Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 7 Jan 2022
I am resisting the temptation to copy all of those, that should be in a text or spreadsheet file and attached here, anyway.
If the time stamps are read in as character arrays (they should be), this should work —
C = {'13:31:39' 0
'13:31:49' 8.1
'13:31:59' 15.3
'13:32:09' 24.4
'13:32:19' 30.8
'13:32:29' 38.3
'13:32:39' 44
'13:32:49' 50.4
'13:32:59' 57.6};
a = table(C(:,1),cell2mat(C(:,2)), 'VariableNames',{'TimeStamps', 'Head(mm)'}) % Replaces The 'readtable' Call
a = 9×2 table
TimeStamps Head(mm) ____________ ________ {'13:31:39'} 0 {'13:31:49'} 8.1 {'13:31:59'} 15.3 {'13:32:09'} 24.4 {'13:32:19'} 30.8 {'13:32:29'} 38.3 {'13:32:39'} 44 {'13:32:49'} 50.4 {'13:32:59'} 57.6
a.TimeStamps = datetime(a.TimeStamps, 'InputFormat','HH:mm:ss', 'Format','HH:mm:ss')
a = 9×2 table
TimeStamps Head(mm) __________ ________ 13:31:39 0 13:31:49 8.1 13:31:59 15.3 13:32:09 24.4 13:32:19 30.8 13:32:29 38.3 13:32:39 44 13:32:49 50.4 13:32:59 57.6
VN = a.Properties.VariableNames;
figure
plot(a.TimeStamps, a.('Head(mm)'))
grid
xlabel(VN{1})
ylabel(VN{2})
Experiment as necessary to get the desired result.
.
  4 Comments
Jon
Jon on 7 Jan 2022
When I made a simple example using the attached .xlsx file and read the data in using (following the OP's example which I was trying to reproduce the problem with)
files = ['Head_Timestamps.xlsx'];
a = readtable(files);
x = a{:,1};
y = a{:,2};
I found that the resulting time values, x, were serial datenumbers (fractions of a 24 hour day). Do you know why they didn't get read in as a character array, as it looks like you expected?
Thanks,
Jon

Sign in to comment.

More Answers (1)

Jon
Jon on 7 Jan 2022
I copied and pasted your data into an .xlsx file (attached here) and ran your commands (only modified to have the .xlsx file in the local directory)
files = ['Head_Timestamps.xlsx'];
a = readtable(files);
x = a{:,1};
y = a{:,2};
figure()
plot(x,y)
and got the following plot, without errors. So I am unable to reproduce your problem. Maybe there some difference in formatting or something like that between my .xlsx file and the one you are using. Please have a look and see if you can make a small self contained example to reproduce the problem.
I'm not sure what you mean by having "reached your limit of sharing files", but you should be able to attach a small .xlsx file as I have here. Please attach a small file that reproduces your problem. Also, I suggest using the code button on the MATLAB answers toolbox to get your code nicely formatted, and to make it easy to cut and paste out of the post.
  1 Comment
muhammad choudhry
muhammad choudhry on 7 Jan 2022
Cannot attach this file because:
  • You are limited to 10 daily uploads. If you need to upload additional files, delete one or more files now or wait 24 hours to upload more files.
That's a message appearing. What version of matlab are you using ? I tested again its not working! Why there are numbers in your x-axis instead of timestamps?

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by