Clear Filters
Clear Filters

Accessing and synchronizing timetables in cell array?

19 views (last 30 days)
I've been working on a script to examine salinity around specific time ranges around high tide (e.g. +/- 30 min, 1 hour, 2 hours, etc), and I'm trying to keep it as systematic as possible so that I can import different dates/locations and run it without any changes. Right now I'm at the point where I have a 296x1 cell array (mr2), with a 5x1 timetable in each cell. Essentially, I'm trying to extract the timetables from this cell array to create one combined timetable. Each timetable has the same variable, and the timestamps are chronological going through cells 1:296. I've been attempting this through a for loop, although I think I'm having an issue with the indexing.
%% Convert cell array to timetable
lengthmr = length(mr2).*5;
iterations = zeros(lengthmr,1);
sz = [lengthmr 2];
varTypes = ["datetime","double"];
varNames = ["Timestamp","Sal_psu"];
T = table('Size',sz,'VariableTypes',varTypes,'VariableNames',varNames);
for i = 1:length(iterations)
T(i,"Sal_psu") = mr2{i,1}(i,1);
end
The error message I receive is:
Error using ()
Row index exceeds table dimensions.
Error in COMPARE_salinity_model_MRB_v2m (line 46)
T(i,"Sal_psu") = mr2{i,1}(i,1);
So I know the indexing I'm doing is quite off, but I've been trying to troubleshoot for a few days now and thought I'd reach out to the community. I will also attach the variables to run this bit of code.
TIA!
  1 Comment
Mikaela Martiros
Mikaela Martiros on 6 Feb 2023
Also, apologies if this is a bit clunky! I tend to write scripts by working through my logic step by step, so I often have a lot of variables or a lot of steps for fairly simple processes. I usually work these out after getting what I need from the code, but yeah, be gentle!

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 7 Feb 2023
Edited: Stephen23 on 7 Feb 2023
Do not use a loop for this! The simple MATLAB approach is to use a comma-separated list:
For example:
S = load('salinity_code_troubleshoot.mat');
mr2 = S.mr2
mr2 = 296×1 cell array
{5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable} {5×1 timetable}
T = vertcat(mr2{:}) % comma-separated list
T = 1480×1 timetable
Timestamp Sal_psu ___________________ _______ 2022-05-12 19:30:00 6.25 2022-05-12 19:45:00 7.09 2022-05-12 20:00:00 7.78 2022-05-12 20:15:00 8.2 2022-05-12 20:30:00 8.37 2022-05-13 08:00:00 6.54 2022-05-13 08:15:00 7.13 2022-05-13 08:30:00 7.82 2022-05-13 08:45:00 8.04 2022-05-13 09:00:00 8.1 2022-05-13 20:30:00 8.63 2022-05-13 20:45:00 9.38 2022-05-13 21:00:00 9.82 2022-05-13 21:15:00 10.08 2022-05-13 21:30:00 10.22 2022-05-14 08:45:00 6.32
  2 Comments
Mikaela Martiros
Mikaela Martiros on 7 Feb 2023
Beautiful! Always learning, thank you for the solution and documentation links!

Sign in to comment.

More Answers (1)

Luca Ferro
Luca Ferro on 7 Feb 2023
Edited: Luca Ferro on 7 Feb 2023
[cMax,~]=size(mr2);
C=mr2{1} %needs the first 5 elements of the table as preallocation
for ii=1:cMax
C=[C ; mr2{ii}]; %vertically concatenate tables
end
C=sortrows(C); %the data in mr2 is not sorted timewise, so if you want it sorted add this line
Hopefully this is what you were looking for.

Categories

Find more on Cell Arrays in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!