Plot 2 traces starting at a zero time point

1 view (last 30 days)
Brittany
Brittany on 2 Aug 2014
Commented: dpb on 4 Aug 2014
Hello,
I am interested in plotting 2 traces vs. time. Specifically, with the time values starting at zero. The values are imported from sensor output files that log a timestamp when collected. The problem is that the sensors start recording at different times (ie sensor 1 10:00:57 PM, sensor 2 10:01:23 PM). I would like the graph to start when sensor 2 starts. So instead of the first value for sensor 2 being (phase, 10:01:23 PM) it would be (phase, 0).
I would also like to use the code for future sensor output files so unfortunately subtracting by 10:01:23 isn't an option. Is there a way to graph these two traces while still maintaining the time, phase relationship?
Here is the code I have generated:
% Open ASCII File and read Time and Phase Number data
fid=fopen('Sensor 1');
fmt = ('%s%s%*s%*s%f%*s%*s%*s');
data = textscan(fid,fmt,'headerlines',39,'collectoutput', true,'delimiter',';');
% Convert cell array to numerical array
phase = cell2mat(data(:,2));
% Convert dates to serial date numbers
dn = datenum([char(data{1,1}(:,1)) char(data{1,1}(:,2))],'mm/dd/yyyyHH:MM:SS AM');
% Close File
fid=fclose(fid);
% Open Second ASCII File and read Time and Phase Number data
fid=fopen('Sensor 2');
fmt = ('%s%s%*s%*s%f%*s%*s%*s');
data = textscan(fid,fmt,'headerlines',39,'collectoutput', true,'delimiter',';');
% Convert cell array to numerical array
phase2 = cell2mat(data(:,2));
% Convert dates to serial date numbers
dn2 = datenum([char(data{1,1}(:,1)) char(data{1,1}(:,2))],'mm/dd/yyyyHH:MM:SS AM');
% Close File
fid=fclose(fid);
%Plot both phase traces on graph with time
plot(dn,phase,dn2,phase2)
datetick('x', 'HH:MM PM')
title('Phase Number vs. Time')
xlabel('Time')
ylabel('Phase Number')
I have also attached the two sensor output files.
Thanks! B
  15 Comments
Star Strider
Star Strider on 4 Aug 2014
You need to NaN it out:
ddn2 = diff(dn2);
cdn2 = find( (ddn2 > mean(ddn2)+10*std(ddn2)) | (ddn2 < mean(ddn2)-10*std(ddn2)));
phase2(cdn2) = NaN;
dn2(cdn2) = NaN;
This is the discontinuous time stamp in the Sensor_2 data. Rather than correcting it, I just deleted it and the phase2 value for those two points. (The decision criteria are arbitrary but functional.) Manually correcting it would also work.
dpb
dpb on 4 Aug 2014
Why not just fix the time data, Star? It's essentially trivial once you've got datenums, anyways...
idx=find(diff(dn)<0)+1;
dn(idx)=dn(idx)+1;
This would fix 'em all in a file that happens to extend over more than a given day, if that were to ever happen as well...
We can't seem to get on a similar wavelength here altho generally are quite self-consistent... :)

Sign in to comment.

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!