Reputation: 51
I have two sets of data: one with 120 data points per day (10 minute logging intervals) and one with 96 data points per day (15 minute logging intervals).
I already have a functional matlab script to plot two data files with the same number of points in a 24 hour period. The problem is that this script relies on the number of points per day to label the x-axis.
The main bit of the code is as follows:
data1=importdata(a);
data2=importdata(b);
data=data1.data(:,1);
Data=data2.data(:,1);
text=data1.textdata;
for i=1:length(data(:,1))
dates(i,:)=[str2num(text{i,1}(1:2)),str2num(text{i,1}(4:5)),str2num(text{i,1}(7:8))];
dates2(i,:)=text{i,1};
time(i,:)=[str2num(text{i,2}(1:2)),str2num(text{i,2}(4:5)),str2num(text{i,2}(7:8))];
time2(i,:)=text{i,2};
end
pH=data(:,1);
pH2=Data(:,1);
maxpH=max(data(:,1));
minpH=min(data(:,1));
h=figure;
set(h,'Position', [1 1 1200 565]);
subplot('Position',[0.08 0.09 0.87 0.8]);
hold on
set(gcf,'PaperPositionMode','auto');
plot(1:length(data(:,1)),acidosisthreshold*ones(1,length(data(:,1))),'r','linewidth',2);
plot(pH,'linewidth',1.5);
plot(pH2,'m','linewidth',1.5);
set(gca, 'XTick',1:pointsperday:length(dates), 'XTickLabel',dates2(1:pointsperday:length(dates(:,1)),:),'xlim',[0 length(dates)]);
xlabel('Time', 'FontSize',16);
ylabel('pH', 'FontSize',16);
title(['A plot of the pH over ',num2str(c),' days; group']);
As you can see, the pointsperday variable is what sets the scale on the x-axis. My question is: how can I adapt this to work for two files with different values for pointsperday?
edit: Data is read in from a csv document. An example line would be:
15/01/2014 , 00:00:00 , 6.53 , -330 , 39.7 , 2.97
which corresponds to:
DD/MM/YYYY , HH:MM:SS , pH , [other values are irrelevant
]
Upvotes: 0
Views: 169
Reputation: 221534
You may use the following code and yes, it's a bit dirty, but works (based on the input data that I could assume from your input info) -
%% Import data
for i=1:length(data(:,1))
dates(i,:)=[str2num(text{i,1}(1:2)),str2num(text{i,1}(4:5)),str2num(text{i,1}(7:8))];
dates2(i,:)=text{i,1};
time(i,:)=[str2num(text{i,2}(1:2)),str2num(text{i,2}(4:5)),str2num(text{i,2}(7:8))];
time2(i,:)=text{i,2};
end
pH=data(:,1);
pH2=Data(:,1);
maxpH=max(data(:,1));
minpH=min(data(:,1));
h=figure;
set(h,'Position', [1 1 1200 565]);
subplot('Position',[0.08 0.09 0.87 0.8]);
%% Correction needed to normalize sizes of pH and pH2
ext_len = max(size(pH,1)*2,size(pH2,1)*3);
a1 = NaN(ext_len,1);
a2 = NaN(ext_len,1);
a1(2:2:end) = pH;
a2(3:3:end) = pH2;
pH = a1;
pH2 = a2;
data=pH;
dates1=NaN(ext_len,size(dates,2));
dates1(2:2:end,:) = dates;
dates=dates1;
%% Continue plotting
hold on
set(gcf,'PaperPositionMode','auto');
plot(1:length(data(:,1)),acidosisthreshold*ones(1,length(data(:,1))),'r','linewidth',2);
plot(pH,'b.','linewidth',1.5);
plot(pH2,'m.','linewidth',1.5);
set(gca, 'XTick',1:pointsperday:length(dates), 'XTickLabel',dates2(1:pointsperday:length(dates(:,1)),:),'xlim',[0 length(dates)]);
xlabel('Time', 'FontSize',16);
ylabel('pH', 'FontSize',16);
As you can see, we were needed to normalize (i.e. make the sizes of pH and pH2) same and for that the intermediate data have to be filled with NaNs.
Also, you can see the plot would use dots as compared to the default lines. If you have to use plot with lines, this code could be looked into.
Let us know if the above code works for you!
Upvotes: 0
Reputation: 2131
Without knowing the details of your data format, it is difficult to give definitive advice. What I usually do in situations like this is to read the date/time string and convert to Matlab's internal representation, using datenum()
. This is usually fairly clever about the string format, and it can be given an explicit format string if necessary.
The data may then be plotted using the datenum
format data as the x
value. This is independent of the time interval between the points in each series. The x ticks can be generated using datetick()
.
Alternatively, you can work by finding the earliest data point and subtracting its datenum
from all the others and using the result as the x
value. This gives you a scale of days since the start of the data.
Edit:
If you catenate the first two fields together, to give an input string like 15/01/201400:03:15
then you can get a datenum
by:
timestamp = datenum(input_string, 'dd/mm/yyyyHH:MM:SS');
Upvotes: 1