Plotting vectors with different lengths due to missing values

4 views (last 30 days)
I have sets of data that I need to plot against each other, the problem is that some of the data points are present in one set but were not recorded in another (same sample but different type of data) I was wondering if there was any way for when one data point is not present (I have denoted this with a % on the lines without data) to discard the other data points from that sample. Currently MATLAB is just ignoring the data line with the % and plotting data points against each other that are not from the same sample. I hope this is clear enough. Cheers in advance

Answers (1)

Kelly Kearney
Kelly Kearney on 10 Jul 2017
Can you give a small sample of your data? Is it manually entered in a script, or are you reading it in from a file?
In general, you can use NaNs as placeholders for missing data. But the best way to do this will depend on the exact format of your data.
  2 Comments
Jonas Joubert
Jonas Joubert on 11 Jul 2017
I'm reading the data from a file. Here's two examples of the data files I'm using, as you can see the ammonia file is missing data from samples 19 and 23. This is the code I'm using to read in the data as well
function [d,mean,SE] = freadavonboatdata(infile)
data = load(infile);
d = data(:,2);
mean = data(:,3);
SE = data(:,5);
n = length(d);
end
end
Kelly Kearney
Kelly Kearney on 11 Jul 2017
Well, that can't be the exact code you're using to read in those files... load will fail (or at least, only read part of the file) if you have text fields like 'NO DATA' mixed into your data.
In this case, I'd use a table array to read in the data; readtable can handle missing data pretty well, and the table join command makes it easy to combine two datasets that share a common key variable (in this case, Sample).
files = {...
'~/Downloads/Ammonia.txt'
'~/Downloads/Conductivity.txt'};
nfile = length(files);
tbl = cell(nfile,1);
for ii = 1:length(files)
tbl{ii} = readtable(files{ii}, 'headerlines', 1, 'treatasempty', 'NO DATA');
isemp = isnan(tbl{1}.Sample);
tbl{ii} = tbl{ii}(~isemp,:);
end
A = join(tbl{1}, tbl{2}, 'keys', 'Sample');
Once all your data is in the table, you can plot it easily, either with or without the Sample numbers that include missing data.
subplot(2,1,1);
[~, hln(1), hln(2)] = plotyy(A.Sample, A.Mean_left, A.Sample, A.Mean_right);
set(hln, 'marker', 'o');
title('Missing data points included (as NaNs)');
ismiss = any(ismissing(A(:,{'Mean_left','Mean_right'})),2);
subplot(2,1,2);
[~, hln(1), hln(2)] = plotyy(A.Sample(~ismiss), A.Mean_left(~ismiss), A.Sample(~ismiss), A.Mean_right(~ismiss));
set(hln, 'marker', 'o');
title('Sample dropped if missing in either dataset');

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!