MATLAB: How to reshape 3 vectors (date, depth, and variables) into a matrix separated by day

ctd profiledailyoceanographyreshapetime series

This is my first question, and I’m struggling for an eloquent way to do this. I have a large dataset of vectors separated by parameter, and I would like to reshape into a matrix in order to do 3D Matlab manipulations. For example, I get an error if I try to use pcolor with 3 vectors. I’ve attached a data sample.
I’d like to end up with a matrix where X = Datetime (separated by day), Y = water depth, and Z = variable (such as water temperature, salinity, etc.).
Here is a snapshot of the existing data format, where size of each vector = 7 x 1:
SampleDate = [01/28/2014 12:13:37; 01/28/2014 12:14:58; 01/28/2014 12:15:20; 02/06/2014 11:02:42; 02/06/2014 11:05:59; 02/06/2014 11:06:24; 02/06/2014 11:06:32];
Depth = [1.5; 5; 10; 1; 5; 10; 12];
SampleTemperature = [8.22; 8.29; 8.29; 7.72; 7.88; 7.9; 7.92];
I would like to transform to a matrix, so that I end up with instead a size of 4 x 2. For example,
SampleDate = 01/28/2014 12:13:37 02/06/2014 11:02:42
01/28/2014 12:14:58 02/06/2014 11:05:59
01/28/2014 12:15:20 02/06/2014 11:06:24
NaN 02/06/2014 11:06:32
SampleTemperature = 8.22 7.72
8.29 7.88
8.29 7.9
NaN 7.92
I recognize that I will have to have NaN values if some of the vectors are shorter than others, and the length of the matrix will be equal to the max of the number of samples per day, so I put this in the example.
I will be filtering the data first so that I use only the value of “Down” for the down cast results. Below is the import code that I have currently to load the attached file.
[file, pathname] = uigetfile({'*.*', 'All Files (*.*)'}, 'Load the CTD profile xls file');
fid = fopen(file);
headerline = fgetl(fid);
formatSpec = '%s%s%f%s%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%f%s%s%s';
data = textscan(fid,formatSpec,'Delimiter','\t');
fclose(fid);
%Pull out the row with the headers
headers = textscan(headerline,'%s','Delimiter','\t');
%Remove special characters from the header text
headers{1,1} = regexprep(headers{1,1},'[%!/()^, =]','');
%Convert 1st column into the date time format
data{2} = datetime(data{2}, 'Format', 'MM/dd/yyyy HH:mm:ss', 'InputFormat', 'M/d/yyyy h:mm:ss a');
%Create a structure array
datastr = struct();
for i=1:numel(headers{:})
datastr.(headers{1}{i}) = data{i};
end
%Next, index so looking at the down cast only
idxD = ~cellfun('isempty',strfind(datastr.Updown,'Down'));
%Pull out vectors of interest
SampleDate = datastr.Sampledate(idxD);
Depth = datastr.Depth(idxD);
SampleTemperature = datastr.SampleTemperatureFielddegC(idxD);
clear ans fid file formatSpec headerline k n tline i pathname data headers;

Best Answer

clc; clear all;
SampleDate = [{'01/28/2014 12:13:37'}; {'01/28/2014 12:14:58'}; {'01/28/2014 12:15:20'}; {'02/06/2014 11:02:42'};
{'02/06/2014 11:05:59'}; {'02/06/2014 11:06:24'}; {'02/06/2014 11:06:32'}];
Depth = [1.5; 5; 10; 1; 5; 10; 12];
SampleTemperature = [8.22; 8.29; 8.29; 7.72; 7.88; 7.9; 7.92];
%%seperate dates by day
data = datevec(SampleDate) ;
days = data(:,2) ;
[c,ia,ib] = unique(days) ;
%%Get indices of repeated values
N = length(c) ;
idx = cell(N,1) ;
for i = 1:N
idx{i} = days==c(i) ;
end
%%seperate the given arrays
SD = cell(N,1) ; % sample date reshaped
D = cell(N,1) ; % sample depth reshaped
ST = cell(N,1) ; % sample temperature reshaped
for i = 1:N
SD{i} = SampleDate(idx{i}) ;
D{i} = Depth(idx{i}) ;
ST{i} = SampleTemperature(idx{i}) ;
end
Your data is reshaped into cells, you can access those using SD{1}, D{2} etc,.